Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Write a program that will create an integer array with 1000 entries.
After creating the array, initialize all of the values in the array to 0. Next, using the rand function, loop through the array and save a random number between 1 and 10 in each entry of
the array.

What I have tried:

I have know idea on what to do or how to start it please help
Posted
Updated 14-Mar-18 19:20pm
v2
Comments
Mycroft Holmes 14-Mar-18 23:44pm    
Nick this is a straight up study question, a rather basic one at that. It will almost certainly be covered by you study material. Or you could search for working with arrays and study some of the results.

Quote:
I have know idea on what to do or how to start it please help

NO! You try to make us believe that you never had a single course about C programming and that the teacher just trowed a random homework unrelated to any course you have done so far.
Quote:
how to start

You start to write this C program exactly like any C program you ever seen or written.

This requirement is 1 of the most simple you will ever see. read carefully and start to work, it really not complicated.

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.
 
Share this answer
 
this generates 1000 random numbers everytime, but the only thing is that the average is always 5(did not run as many times so there might be an average other than 5)
done on linux ubuntu 16.04, codeblocks, also no need to initialize every value in the array to 0. Hope this helped
C++
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main(){
    srand(time(NULL));
    int a[1000], sum=0,num[10];
    float avg;
    for(int i=0; i<1000; i++){
        a[i]=rand()%10+1;
    }
    for(int j=0; j<999; j++){
        sum+=a[j];
    }
    avg=sum/1000;
    for(int m=0; m<10; m++){
        num[m]=0;
    }
    for(int k=0 ;k<999; k++){
        for(int n=0; n<10; n++){
            if(a[k]==n+1){
                num[n]++;
            }
        }
    }
    cout<<"Sum "<<sum<<endl<<"average "<<avg<<endl;
    for(int x=0; x<10; x++){
        cout<<"number "<<x+1<<" was generated "<<num[x]<<" times\n";
    }
}
 
Share this answer
 
v2
Comments
Patrice T 15-Mar-18 2:00am    
"Hope this helped"
Homework is about training/practicing. With a flow blowup solution, the only training the OP got is begging, and I fear ot will not help for next assignment.
Richard MacCutchan 15-Mar-18 6:05am    
You do not help people by doing their work for them.
Rick York 15-Mar-18 16:57pm    
"no need to initialize every value in the array to 0."

There is if his assignment requires it and his does. It is second in the list of requirements.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900