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.
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:
Get Answers For Free
Most questions answered within 1 hours.