passing a function to an array.

Removed
Last edited on
This is your program (with two extra lines):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include<ctime>

void fillArray(int);

int main()
{
  srand( time(0) );
  const int courses= 5;
  std::string course_name[courses] {"C++", "Python", "JavaScript", "CSS", "HTML" };
  for ( int count=0; count < courses; ++count) {
    std::cout << "Course " << count << " start:\n";
    std::cout << "The number of students in " << course_name[count] << " is ";
    for ( int index=0; index < courses; ++index ) {
      std::cout << 10 + (rand() %20) << '\n';
    }
    std::cout << "Course " << count << " end\n\n";
  }
}

Course 0 start:
The number of students in C++ is 18
18
13
12
16
Course 0 end

Course 1 start:
The number of students in Python is 25
12
28
15
29
Course 1 end

Course 2 start:
The number of students in JavaScript is 15
18
27
14
16
Course 2 end

Course 3 start:
The number of students in CSS is 12
13
15
14
13
Course 3 end

Course 4 start:
The number of students in HTML is 14
24
20
29
12
Course 4 end


I don't see "five numbers in front of each course".


Array is data. Data does nothing. Functions do use, modify, generate data. You can pass data to function and receive data from function.
Actually the out put should be like :
The number of students in C++ is 18
The number of students in Python is 25
and so on, and every time a run the program it should generate a different number of students.
Please use the [code][/code] tags when posting code.

Your code, with some comments for your information.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include<fstream>
#include<cstdlib>
#include<ctime>
using namespace std;


fillArray(int[], int);
int main()
{
  int count;
  const int courses = 5;
  string course_name[courses] =
      { "C++", "Python", "JavaScript", "CSS", "HTML" };
  int numStudents[courses];
  for (count = 0; count < courses; count++) {
    cout << "The number of students in " << course_name[count] << " is ";
    //!! 1. call fillArray before trying to print things.
    //!! 2. Output the numStudents array element
    fillArray(numStudents, courses);
  }

  return 0;
}

//generate a random number
void fillArray(int numbS[], int size)
{
  //!! Move srand to the start of main, so you won't
  //!! be tempted to call it more than once in a program
  srand(time(0));
  for (int index = 0; index < size; index++) {
    //!! instead of printing, just store them
    //!! in your supplied array
    cout << 10 + (rand() % 20) << endl;
  }
}
Thank you Brother, it worked. I like your approach, it was very clever.
Please DON'T remove your question after getting an answer. It makes the thread useless as a learning resource for others. It's a selfish abuse of the forum, and of its users.
I'm sorry. you can't just copy from my code, you have to make your own effort. I don't think you need to see my code though, coz you have enough infos here if you just wanna understand the concept!!!!!!
But soon enough, people will learn "oh, it's Algonology the thread trasher" and pass you over for someone more in tune with the forum ethos.
I'm sorry. you can't just copy from my code, you have to make your own effort. I don't think you need to see my code though, coz you have enough infos here if you just wanna understand the concept!!!!!!

The purpose of leaving your code there isn't so that people can copy it - and what hubris you have in thinking that people will want to! The purpose is so that this thread makes sense as something other people can learn from.

That's the quid pro quo for getting help here. It's your contribution to this forum, in return for people on this forum giving up their time and effort to help you.

As Salem C says, if you don't make that contribution, then people here won't want to help you. Assuming you don't get banned before that happens.
Algonology have you ever tried Googling for something related to C++? Have you ever come across a forum when you were searching (anything with public discussion for that matter)?

Now think for a second, would you have been able to learn anything if the owner of the thread could just "delete" the thread after he got his answer? And think about this, were you "copying" the entire coding example?

This is a public forum, people come here to learn and to help others. You're lucky to have so many experienced programming wizs in this forum who are willing to help. Remember they have NOTHING to gain by helping you, but they help you because they are kind enough to. They are NOT helping you so that you can delete your post 2 seconds later but so that your post might help not only you but some other people too, or maybe spark some valuable conversation.

But hey aren't you copying from salem c and keski!? They gave you your code and you're just copying it!! Why doesn't it count when others are helping you?
Topic archived. No new replies allowed.