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

0 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 <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

int main()
   for (int i = 1; i <= 10; i++) {
       printf("%d ", 1 + (rand() % 10));
   return 0;

what i would like to know is how to implement that into a vector, and find the sum and median value of said vector.  

If anyone could help me with this I'd really appreciate it.

NOTE: I am currently not in this course and this will not be considered cheating if someone helps me with the code.

Post a Reply


Oldest  Newest  Rating
0 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 direction

int array[10];
int ii, sum;

// populate the array.
for (ii=0; ii<10; ii++)
  array[ii] = 1+ rand() % 10;
  printf ("%d ", array[ii]);

// find sum
for (ii=0, sum=0; ii<10; ii++)
  sum += array[ii]);

printf("sum: %d\", sum);
  • 1



Used in many types of software including music players, video games, and many large scale applications.

Bucky Roberts Administrator