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...
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 C++ program to do the following: Declare and assign values to int variables x,...
Write a C++ program to do the following: Declare and assign values to int variables x, y Declare an int variable z; and store the sum of x and y in it Declare a pointer called pz and point it in the heap direction (using new) Store the z value in the heap location using the pz pointer Print the content of the pz pointer Print the pointer (the address)
C++ Code! Represent the following matrix using a two-dimensional array and write a nested for loop...
C++ Code! Represent the following matrix using a two-dimensional array and write a nested for loop to initialize the elements (do not initialize the array in the declaration): 10  9   8 7   6   5 4 3   2
C program question: Write a small C program connect.c that: 1. Initializes an array id of...
C program question: Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and changing all the entries with the same name as p to...
in c++ Write a C++ program that asks the user to enter an integer number and...
in c++ Write a C++ program that asks the user to enter an integer number and prints it back "vertically" to the screen. Use recursion in your solution. As an example of how the program will work: Please enter an integer: 12345 The integer you entered will print vertically as: 1 2 3 4 5