#include <iostream>
main()
{
int intarray[10]={0};
int x = 0;
int average = 0;
for (x; x < 10; x++)
{
intarray[x] = rand();
average += intarray[x];
}
std::cout << average/10 << std::endl;;
return 0;
}
is this correct
i need to create an integer array that holds 10 random values. Create a loop to find the average of those 10 values and print the result.
i was wondering if my code was correct
Looks alright to me, why don't you try running it and see if it works? (Unless you know of some specific problem with it already...)
int intarray[10]={0};
I don't think that's valid. Try:
Also, you have one too many semicolons on the second last line.
I don't think that's valid. Try:
int intarray[10];
Also, you have one too many semicolons on the second last line.
it works i was just checking to see if i was doing it the right way and to make sure i understood the assignment thats all
thanks for the reply
int intarray[10]={0};
that line initializes the whole array to zero
thanks for the reply
int intarray[10]={0};
that line initializes the whole array to zero
You allways get the same number becouse you dont seed the random number generator. You should call srand() with a different value every time at the start of the program.
Seed it with the system time, since that changes everytime you run the program:
#include <time.h>srand( (unsigned)time( NULL ) )
Quote:Original post by ostamo1
it works i was just checking to see if i was doing it the right way and to make sure i understood the assignment thats all
thanks for the reply
int intarray[10]={0};
that line initializes the whole array to zero
Actually it only sets intarray[0] to 0. YOur compiler in debug mode is likely zeroing the array, so it looks zeroed out to you. Try changing it to int intarray[10]={1}; and you will see what I mean. The correct way to zero it is with meset, a for loop or int array[0]={0,0,0,0,0,0,0,0,0,0}; .
// Only the first element will be one#include <iostream>int main(){ int a[10]={1}; for (int i=0; i<10; ++i) std::cout << "a["<<i<<"]="<< a<< std::endl;; std::cin.ignore(); return 0;}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement