Question

Write a C++ program to perform the following tasks     a) Declare an integer array of...

Write a C++ program to perform the following tasks

    a) Declare an integer array of size 1000.
    b) Initialize the array with random values between 1 and 9.
    c) Write the code to find and print, how many 1’s occur in the array.

Homework Answers

Answer #1

Code For Above Problem:

#include <iostream>
#include <stdlib.h>
#include <ctime>
using namespace std;
int main()
{
    srand((unsigned) time(0));
        int array[1000];//Declare an integer array of size 1000.
        int temp;

        //Initialize the array with random values between 1 and 9.
        for(int i=0;i<1000;i++)
    {
        temp=1 + (rand() % 9);
        array[i]=temp;
    }
    
        //initialize count to 0 to hold occurences of 1's in array
    int count=0;
        //Look for all values in array
        for(int i=0;i<1000;i++)
        {
                //if current value is 1 increment count
                if(array[i]==1)
                {
                        count++;
                }
        }
        //printing how many 1’s occur in the array 
        cout<<"Number of 1's occures in array: "<<count<<endl;
    return 0;
}

Some Sample Run Results:

Run1:

Number of 1's occures in array: 98

Run2:

Number of 1's occures in array: 112

Run3:

Number of 1's occures in array: 85

Image Of Code:

Image Of All Sample Run Results:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write the following program in MIPS: a) declare an array A of the following numbers: 3,...
Write the following program in MIPS: a) declare an array A of the following numbers: 3, 5, 8, 10, 12, 2, 76, 43, 90, 44 b) declare a variable called size which stores the number of element in array A, that is 10. c) write a subroutine to search for a number stored in an array and return true or false. In C++ the subroutine is as follows: search(array, size, number_To_Search) e.g. search(A, 10, 12) The subroutine should return 0...
Write a program in c++ to Convert an array of inches to an array of centimeters....
Write a program in c++ to Convert an array of inches to an array of centimeters. The program should contain a function called inchesTOcm with three parameters (inches array that contains the values in inches, cm array to save the result in, and an integer variable that defines the length of the array). In the main function: 1. Define an array (inches) of length 3. 2. Initialize the array by asking the user to input the values of its elements....
For questions 1 and 2 declare the array size as name constant first then use it...
For questions 1 and 2 declare the array size as name constant first then use it to declare the array 1. Declare an array named scores of type double and size 30. 2. Declare an array named names of string objects and size 15 3. Given the following array definition: int values[] = {2, 5, 8, 11}; What does each of the following display? cout << values[1];         B) cout << value[3]+values[0];   Define a three element array named letters, initialize it...
In c++ Write a program that creates a dynamically allocated array to store students’ grades. The...
In c++ Write a program that creates a dynamically allocated array to store students’ grades. The user should be able to decide the size of the array during run time. Write code to fill this array from the keyboard and then print out the values of your array,
Declare an integer array of size 10 with values initialized as follows. int intArray[] = {1,...
Declare an integer array of size 10 with values initialized as follows. int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Compute each item of a new array of same size derived from the above array by: adding first item (intArray[0]) of the array with 3rd, 2nd with 4th, 3rd with 5th and so on. For the last-but-one item intArray[8], add it with first item and for the last item (intArray[9]) add it with 2nd item...
In this lab, you complete a partially prewritten C++ program that uses an array. The program...
In this lab, you complete a partially prewritten C++ program that uses an array. The program prompts the user to interactively enter eight batting averages, which the program stores in an array. The program should then find the minimum and maximum batting average stored in the array as well as the average of the eight batting averages. The data file provided for this lab includes the input statement and some variable declarations. Comments are included in the file to help...
Write JavaScript statements to accomplish each of the following tasks: a) Display the value of the...
Write JavaScript statements to accomplish each of the following tasks: a) Display the value of the seventh element of array f. b) Initialize each of the five elements of one-dimensional array g to 8. c) Total the elements of array c, which contains 100 numeric elements. d) Copy 11-element array a into the first portion of array b, which contains 34 elements. e) Determine and print the smallest and largest values contained in 99-element floatingpoint array w HTML java script...
Write some C++ program segments that solves the indicated tasks (you do not have to write...
Write some C++ program segments that solves the indicated tasks (you do not have to write a complete program, nor be concerned about "good" output; a small code segment will be enough). a) A program that gets a double number from the user, decides whether that number is positive, negative, or zero and display its decision on the screen. a) A function isPositive that takes as input a double number and returns the integer 1 if the number is positive,...
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns...
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns the index of the first occurance of the smallest value in the array. Your function must be able to process all the elements in the array. Create a function prototype and function definition (after the main function). Your main function should declare a 100 element integer array. Prompt the user for the number of integers to enter and then prompt the user for each...
Write a program in Java that creates an array of doubles. The array must have size...
Write a program in Java that creates an array of doubles. The array must have size 1000. Fill this array with random numbers between 0 and 1. Then compute and print the total value of all the elements of the array, the mean, the minimum, and the maximum. Filling the array with random numbers must be done by a method that you define and implement. Computing and printing the statistics must be done by another method, which again you must...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT