Finding the Sum and Median of a Vector Created By a Random Number Generator

 Rob Salzman · April 8, 2015 I am a first year college student who is planning on majoring in computer science.  I am taking my first computer science course over the summer.  My friend is currently in the class I'm going to take.  So I'm trying to use him to learn so its not all a surprise when i actually take the course.  So i look at his assignments from time to time and the assignment he has this week is to create a vector or array with integers made from a number generator.  so far all that he has is this for the number generator#include #include #include using namespace std;int main(){   srand(time(0));   for (int i = 1; i <= 10; i++) {       printf("%d ", 1 + (rand() % 10));          }   cout<

Replies

 Yaron X · April 10, 2015 here is some code to populate the array and calculate the sum. You can add the code to find the median yourself :-)I didn't compile or test it, but it should give you a directionint array[10];int ii, sum;// populate the array.srand(time(0));for (ii=0; ii<10; ii++){  array[ii] = 1+ rand() % 10;  printf ("%d ", array[ii]);}printf("\n");// find sumfor (ii=0, sum=0; ii<10; ii++)  sum += array[ii]);printf("sum: %d\", sum);
• 1

C++

129,799 followers