Guys, I have this question, when I run this program, the program suddenly stop and tell me it cannot be runned, could your please tell me my error thanks.

Here is the code:

#include <iostream>

using namespace std;

int main()
{
    int student,subject;
    int marks[student][subject];
    int i,count;

    student=5;
    subject=3;


    //Getting the marks
    for(i=0;i<5;i++)
    {
        for(count=0;count<3;count++)
        {
            cout<<"Enter the marks for student: " << i+1<<endl;
            cout<<", " <<"subject" << count+1<<": "<<endl;
            cin>>marks[i][count];
        }
    }


    //Displaying the marks
    cout<<"n\t\tSubject 1\tSubject 2\tSubject 3"<<endl;
    for(i=0;i<5;i++)
    {
        cout<<"Marks for student "<< i+1 <<endl;
        cout<<"\t";
        for(count=0;count<3;count++)
        {
            cout<<"   " << marks[i][count]<<"\t\t"<<endl;
        }
        cout<<"\n";
    }
    return 0;
}

How big is this array?

int marks[student][subject];

Neither student nor subject are not initialized. You initialize them after you create your array.

How big is this array?

int marks[student][subject];

Neither student nor subject are not initialized. You initialize them after you create your array.

Thank you very much for helping me:)

I have another question, if I wanted to change the format into functions, which variable should i use?


void getmarks(int marks[student][subject]);

or

void getmarks(int marks);

or other solutions?

Please help me out thanks:)

Is there any way to convert it to 2 functions,

function 1 to get marks and function 2 to display the marks?

Here is a smaple which went wrong please correct my mistakes thanks

#include <iostream>

using namespace std;

int getmarks(int marks[]);

int main()
{
    int student,subject;

    student=5;
    subject=3;

    int marks[student][subject];
    int i,count;


    //Getting the marks
    cout<<getmarks(marks[student][subject]);

    //Displaying the marks
    cout<<"\n\t\t\tSubject 1\tSubject 2\tSubject 3"<<endl;
    for(i=0;i<5;i++)
    {
        cout<<"Marks for student "<< i+1;
        cout<<"\t";
        for(count=0;count<3;count++)
        {
            cout<<"   " << marks[i][count]<<"\t\t";
        }
        cout<<"\n";
    }
    return 0;
}


int getmarks(int marks[])
{
    int i,count;
       for(i=0;i<5;i++)
    {
        for(count=0;count<3;count++)
        {
            cout<<"Enter the marks for student: " << i+1;
            cout<<", " <<"subject " << count+1<<": ";
            cin>>marks[i][count];
        }
    }
    return(marks[i][count]);
}

I am still not sure where I have gone wrong:(

int getmarks(int marks[][3]);

Note : You have to specify the column. Thats why the 3 is there.
Although it could be any number.

but in this case, as you have mentioned, I replaced it with

void getmarks(int marks[][3]);

but how do i call out the function?

getmarks(marks);

?

I still got this one final problem before my program can run.

Here is an overview of my program:

#include <iostream>

using namespace std;

void getmarks(int marks[][3]);

int main()
{
    int student,subject;

    student=5;
    subject=3;

    int marks[student][subject];
    int i,count;


    //Getting the marks
    getmarks(marks);

    //Displaying the marks
    cout<<"\n\t\t\tSubject 1\tSubject 2\tSubject 3"<<endl;
    for(i=0;i<5;i++)
    {
        cout<<"Marks for student "<< i+1;
        cout<<"\t";
        for(count=0;count<3;count++)
        {
            cout<<"   " << marks[i][count]<<"\t\t";
        }
        cout<<"\n";
    }
    return 0;
}


void getmarks(int marks[][3])
{
    int i,count;
       for(i=0;i<5;i++)
    {
        for(count=0;count<3;count++)
        {
            cout<<"Enter the marks for student: " << i+1;
            cout<<", " <<"subject " << count+1<<": ";
            cin>>marks[i][count];
        }
    }
    return;
}

The part with problem I think is left with the calling.

It gives me no errors :

#include <iostream>

using namespace std;

void getmarks(int marks[][3]);

int main()
{
   const  int student(5),subject(3);


    int marks[student][subject];
    int i,count;


    //Getting the marks
    getmarks(marks);

    //Displaying the marks
    cout<<"\n\t\t\tSubject 1\tSubject 2\tSubject 3"<<endl;
    for(i=0;i<5;i++)
    {
        cout<<"Marks for student "<< i+1;
        cout<<"\t";
        for(count=0;count<3;count++)
        {
            cout<<"   " << marks[i][count]<<"\t\t";
        }
        cout<<"\n";
    }
    return 0;
}


void getmarks(int marks[][3])
{
    int i,count;
       for(i=0;i<5;i++)
    {
        for(count=0;count<3;count++)
        {
            cout<<"Enter the marks for student: " << i+1;
            cout<<", " <<"subject " << count+1<<": ";
            cin>>marks[i][count];
        }
    }
    return;
}

I have this question,

Is it possible to use


int student,subject;
student=5;
subject=3;
int marks[student][subject]

instead of

const int student(5),subject(3);

int marks[student][subject];

?
If the it is not possible, can your please mind tell me the reason?

In my opinion, I think that it is not possible because we need to indicate the specific array like for example student[5] and subject[3] so that the system would not mixed up the arrays. Am I right? Please respond to me thanks:)

I am also sorry for spamming my post yesterday because I didn't read the post carefully. Sorry and please forgive me thanks:)

int student,subject;
student=5;
subject=3;
int marks[student][subject]

If the it is not possible, can your please mind tell me the reason?

Your compiler might try to explain it like mine does:

error: ISO C++ forbids variable-size array `marks'

I have this question,

Is it possible to use


int student,subject;
student=5;
subject=3;
int marks[student][subject]

instead of

const int student(5),subject(3);

int marks[student][subject];

?
If the it is not possible, can your please mind tell me the reason?

You could use either, both are more or less correct (Note: the first one isn't quite right, but I'll explain shortly!) and achieve exactly the same thing.
The 2nd example you listed is a more terse and therefore makes the code more succinct and to the point. In other words, you're using less code to achieve the same thing.

One thing to note in the first block of code you posted, 'student' and 'subject' should be declared as 'const int', not 'int'.
Arrays must be of fixed size. Declaring 'student' and 'subject' as int indicates to the compiler that the values of 'student' and 'subject' could change, therefore you'll get a compiler error in the line declaring the marks array. The compiler probably won't support the use of variable length arrays. However, changing the types of 'student' and 'subject' to 'const int' will fix the issue!

These lines of code:

const int student(5), subject(3);
int marks[student][subject];

Are creating two constant integer variables. 'student' is initialised to 5 and 'subject' is initialised to 3. Using the const keyword tells the compiler that once set, these values cannot be changed.
Then a two dimensional integer array called marks is then created using the values of student and subject.

Another equally correct way of writing the above code is:

const int student=5, subject=5;
int marks[student][subject];

Yet another way (albeit more long winded, but still correct!):

const int student, subject;
student=5;
subject=3;
int marks[student][subject];

So all three of the above snippets of code do exactly the same thing. But the first two are shorter and more succinct!

In my opinion, I think that it is not possible because we need to indicate the specific array like for example student[5] and subject[3] so that the system would not mixed up the arrays. Am I right? Please respond to me thanks:)

I assume the above comment was aimed at this line of code from your first example:

const int student(5), subject(3);

The thing to take note of here is that the curly braces '()' are being used not the square brackets '[]'. The curly braces indicate a function being called, whereas square brackets indicate that an array is being initialised/used.
As the curly braces are used, then it is not two arrays that are being declared here. It is in fact two functions that are being called.

In both cases, the function in question is the copy constructor for int. The copy constructor takes an int as a parameter and returns a new int with the same value as the value passed in. So for the declaration of 'student' the copy constructor for int is called and passed the integer value 5. So for 'student' a new int is created and given the value 5.
Likewise, for 'subject', the int copy constructor is passed 3, so it returns a new int with the value 3 .

I hope that clears things up for you.
Cheers for now,
Jas.

In my opinion, I think that it is not possible because we need to indicate the specific array like for example student[5] and subject[3] so that the system would not mixed up the arrays. Am I right? Please respond to me thanks:)

Ah, after seeing Sinkulas post (which he wrote while I was composing my previous response, so I didn't see it until I'd posted mine!), I understand what your comment was about now...I see!

Yes you're right. The array needs to be of fixed size.
As I explained in the first part of my post, declaring 'students' and 'subject' as 'int' makes the compiler think that the values could change and will cause the compiler to flag an error at the line declaring the marks array because the arrays size will not considered constant by the compiler.
Declaring 'students' and 'subject' as 'const int' will fix the issue!

Apologies for the long winded explanation of the copy constructor. I understand now that this was not where you were getting confused!
Cheers for now,
Jas.

I am not so clear about this part:

the copy constructor takes an int as a parameter and returns a new int with the same value as the value passed in. So for the declaration of 'student' the copy constructor for int is called and passed the integer value 5. So for 'student' a new int is created and given the value 5.
Likewise, for 'subject', the int copy constructor is passed 3, so it returns a new int with the value 3 .

I would like to ask whether if, this means that when the 'student' is being declared, it will follow the int student which says 5 and the 'subject' will follow the int subject called 3.

And then when the function is being called, the array called 'student' in

int marks[student][subject];

will be equal to 3?

Is it what you mean?

And also I would like to ask

1: how would the system affect the 'student' and 'subject' if the constant is not being placed

2: In nomral application when we use only one array, do we also need to place constant to prevent such problem?

Please reply me as soon as possible thanks:)


Yet another way (albeit more long winded, but still correct!):

const int student, subject;
student=5;
subject=3;
int marks[student][subject];

So all three of the above snippets of code do exactly the same thing. But the first two are shorter and more succinct!

I don't know about your compiler, but mines dosen't allow
declaration of of const object without being initialized. So the
above code would be an error.

Just for the sake of it, take a look at the errors, and see why
one wrong thing can lead to many errors.

error C2734: 'student' : const object must be initialized if not extern
error C2734: 'subject' : const object must be initialized if not extern
error C3892: 'student' : you cannot assign to a variable that is const
error C3892: 'subject' : you cannot assign to a variable that is const
error C2057: expected constant expression
error C2466:cannot allocate an array of constant size 0
error C2466: cannot allocate an array of constant size 0
error C2087: 'marks' : missing subscript
error C2133: 'marks' : unknown size
Comments
Good spot, consider myself rightly corrected!

I am not so clear about this part:

the copy constructor takes an int as a parameter and returns a new int with the same value as the value passed in. So for the declaration of 'student' the copy constructor for int is called and passed the integer value 5. So for 'student' a new int is created and given the value 5.
Likewise, for 'subject', the int copy constructor is passed 3, so it returns a new int with the value 3 .

I would like to ask whether if, this means that when the 'student' is being declared, it will follow the int student which says 5 and the 'subject' will follow the int subject called 3.

If by that what you mean is student will become 5 and subject will
become 3 then yes.

And then when the function is being called, the array called 'student' in

int marks[student][subject];

will be equal to 3?

Is it what you mean?

student will be 5 and subject will be 3. Same as int marks[5][3].
student is the row. Subject is the column.

And also I would like to ask

1: how would the system affect the 'student' and 'subject' if the constant is not being placed

It would result as an error. And would not compile

2: In nomral application when we use only one array, do we also need to place constant to prevent such problem?

Whenever you create an array you have to have its size fixed. So either
declare a const number and place it into an array like so ,

const int NUM = 10;
int Array[NUM] = {0}; //initialize all 10 elements to 0

or have some number inside the brackets, like so :

float Array[10] = {0.0f}; /

You need to have a non-variable number as an argument inside
the array brackets. Otherwise it would be an error. Also note
that arrays have no bound checking. so if you have an array with
some fixed size. And if you (say wanted to print out the array over its fixed size)
then there is no error. Although its a bug. Take a look.

int A[4] = {1,2,3,4};
cout<<A[5]; //no error. Just display junk, or the INT_MAX or INT_MIN

I don't know about your compiler, but mines dosen't allow
declaration of of const object without being initialized. So the
above code would be an error.

Just for the sake of it, take a look at the errors, and see why
one wrong thing can lead to many errors.

error C2734: 'student' : const object must be initialized if not extern
error C2734: 'subject' : const object must be initialized if not extern
error C3892: 'student' : you cannot assign to a variable that is const
error C3892: 'subject' : you cannot assign to a variable that is const
error C2057: expected constant expression
error C2466:cannot allocate an array of constant size 0
error C2466: cannot allocate an array of constant size 0
error C2087: 'marks' : missing subscript
error C2133: 'marks' : unknown size

Oops, you're right!
I forgot about that little rule with consts..
Personally I always initialise constants (and most other variables) at declaration in my own code (like in methods 1 and 2 of my original response). I'd never initialise anything using the third method I posted (be they const or not!). I just unthinkingly slapped the const keyword into the OP's code....
I didn't compile any of the code I posted either....Guess that's something I aught to take the time to do in future! Doh!

Consider myself corrected....heh heh, I really am a window-licker sometimes! ;)

The last two questions, I get what your mean, but for the front part,

Understanding(correct me if I am wrong):

(Q1) "the copy constructor takes an int as a parameter and returns a new int with the same value as the value passed in. So for the declaration of 'student' the copy constructor for int is called and passed the integer value 5. So for 'student' a new int is created and given the value 5.
Likewise, for 'subject', the int copy constructor is passed 3, so it returns a new int with the value 3 ."

This means that the system will take the value 5 and place it inside the marks[subject] and the value 3 and place it inside the [subject]. Because we use int subject=3;

(Q2)If we used a const int subject=3,student=5; This means that we declared that all variables that is named subject will be equal to 3 and hence since the array is marks[student][subject], it will be marks[5][3]

(Q3)The error I had it before. They keep saying that double variable cannot be converted to double * variable(something like that)

(Q4)

const int NUM = 10;/*<--This part in I am not so familiar because I am not taught to use*/
int Array[NUM] = {0}; //initialize all 10 elements to 0

This function is used to say that anything which has the variable called NUM will be 10. Therefore, Array[10]={0};

Usually I use int Array[5]={1,2,3,4}
where the 5 is being given and I just sub it in.

Questions:

(1)Your mentioned that curly braces are used to call out the function so the student(5) and the subject(3) will be a function am I right?

(2)but if that is the case, a function need prototype, implementation too, not only the calling hence how come the function student(5) can be used?

(3)Since student(5) and subject(3) is a function and student will be 5 and subject will be 3. Same as int marks[5][3].student is the row. Subject is the column. Then does it mean that the student(5) and the subject(3) function is to tell the system that the array marks[student][subject] is actually equal to marks[5][3]?

(4)Additional Question:

If I want to multipy an array by itself and then after getting the answer, I wanted the program to only display odd subsripts, ho do I do it?

eg. I use functions and get this array:

//Implementation for the array * array
double SqPrOdd(double values[])
{
    int i;
    for(i=0;i<6;i++)//6 elements inside the array but only display the odd subsripts
    {
        count=values[i];
    }
     return 
}

I already is able to display the output of values[i], ask the user to key in the values 5 times. But the part that I am unclear is how do I calculate an array * the answer of the same array and then display only the odd subscripts.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Understanding(correct me if I am wrong):
(Q1)"the copy constructor takes an int as a parameter and returns a new int with the same value as the value passed in. So for the declaration of 'student' the copy constructor for int is called and passed the integer value 5. So for 'student' a new int is created and given the value 5.
Likewise, for 'subject', the int copy constructor is passed 3, so it returns a new int with the value 3 ."

This means that the system will take the value 5 and place it inside the marks[subject] and the value 3 and place it inside the [subject]. Because we use int subject=3;

(Q1) Not quite, the 'student(5)' and 'subject(3)' parts of that line of code are invoking the copy constructor for the int data-type. So in a very round-about way it's basically saying "set the value of student to be a copy of the number 5" and "set the value of subject to be a copy of the number 3". In other words it's more or less equivalent to using 'student=5' and 'subject=3'.
Then in the line where you have:

int marks[student][subject];

The arrays dimensions will be set to marks[5][3].

(Q2)If we used a const int subject=3,student=5; This means that we declared that all variables that is named subject will be equal to 3 and hence since the array is marks[student][subject], it will be marks[5][3]

(Q2) Yes!

(Q3)The error I had it before. They keep saying that double variable cannot be converted to double * variable(something like that)

(Q3) I can't see any doubles in any of the code you've posted so far, but it sounds like you were passing a double into a function when you needed to be passing a pointer to a double.

(Q4)const int NUM = 10;/*<--This part in I am not so familiar because I am not taught to use*/
int Array[NUM] = {0}; //initialize all 10 elements to 0

This function is used to say that anything which has the variable called NUM will be 10. Therefore, Array[10]={0};

Usually I use int Array[5]={1,2,3,4}
where the 5 is being given and I just sub it in.

(Q4) Yup, pretty much! 'const int NUM = 10;' is creating a constant integer called NUM and initialising it to 10.
The next line 'int Array[NUM]={0};' is creating an array with space for 10 values.
The '={0};' part of the line will initialise everything in the array to 0.

(1)Your mentioned that curly braces are used to call out the function so the student(5) and the subject(3) will be a function am I right?


(2)but if that is the case, a function need prototype, implementation too, not only the calling hence how come the function student(5) can be used?

(3)Since student(5) and subject(3) is a function and student will be 5 and subject will be 3. Same as int marks[5][3].student is the row. Subject is the column. Then does it mean that the student(5) and the subject(3) function is to tell the system that the array marks[student][subject] is actually equal to marks[5][3]?

1. Yes, but the functions in question are not functions called student() or subject(), as I've already explained it is in fact the copy constructor for the int datatype being used.
I can see where you're getting confused as it does look like they are two undefined functions. But trust me, in both cases it's the copy constructor for int being used!

2. The header and implementation are already defined. It is the copy constructor for int which is being used NOT functions called student() and subject()....see above.

3. Yes, more or less. The lines:

const int student(5), subject(3);
int marks[student][subject];

and the lines:

const int student=5, subject=3;
int marks[student][subject];

will be exactly the same as using:

int marks[5][3];

Things any clearer now?
Cheers for now,
Jas.

(4)Additional Question:

If I want to multipy an array by itself and then after getting the answer, I wanted the program to only display odd subsripts, ho do I do it?

eg. I use functions and get this array:
//Implementation for the array * array
double SqPrOdd(double values[])
{
int i;
for(i=0;i<6;i++)//6 elements inside the array but only display the odd subsripts
{
count=values;
}
return
}

I already is able to display the output of values, ask the user to key in the values 5 times. But the part that I am unclear is how do I calculate an array * the answer of the same array and then display only the odd subscripts.

I'm not sure exactly what you're looking for here, but here's a simple example which creates and populates an array of 10 integer values, it then loops through the array and multiplies each item by itself. If the subscript is odd, the value of the current array item is output via the console:

#include <iostream>

using namespace std;

int main()
{
	// declare the maximum size of our array
	const int MAX_ARRAY_SIZE = 10;
	
	// set up our array and give it some values
	int array[MAX_ARRAY_SIZE] = {2,4,6,8,10,12,14,16,18,20};

	// now we'll loop through the array 
	for(int loop=0; loop<MAX_ARRAY_SIZE; ++loop)
	{
		// multiply each value in the array by itself
		array[loop] = array[loop]*array[loop];

		// if the subscript is odd, output the value of the current array position.
		if(loop%2)
			cout << "The value at array position" << loop << "is " array[loop] << endl;
	}

	return 0;
}

After the multiplication, the contents of the array will be:
4, 16, 36, 64, 100, 144, 196, 256, 324, 400

The output from the program is:

The value at array position 1 is 16
The value at array position 2 is 64
The value at array position 3 is 144
The value at array position 5 is 256
The value at array position 7 is 400

Like I said, it's probably not quite what you're after (I wasn't sure exactly what you wanted, the description was a bit difficult to understand), but this example shows you how to multiply the array by itself and display the items with odd subscripts.

To determine whether the subscript is odd, I've used the modulo operator (%).
When you use modulo, it divides one number by another and then outputs the remainder (i.e. 8%3 returns 2 because 8/3=2 remainder 2).
Therefore you can detemine if a number is odd or even by using modulo 2 (%2) with the number.
If number % 2 returns 0, the number is even, if it returns 1 it is odd. (try it yourself, 5%2=1 or 5/2=2 remainder 1) so in lines 20 and 21:

if(loop%2)
    cout << "The value at array position" << loop << "is " array[loop] << endl;

what we're saying is if loop%2 returns 1, then the number is odd, so we want to display the value at the current subscript (loop).

Hope this is of some help,
Cheers for now,
Jas.

The output from the program is:

The value at array position 1 is 16
The value at array position 2 is 64
The value at array position 3 is 144
The value at array position 5 is 256
The value at array position 7 is 400

Oops.. Sorry that's not the output at all!

The correct output is:

The value at array position 1 is 16
The value at array position 3 is 64
The value at array position 5 is 144
The value at array position 7 is 256
The value at array position 9 is 400

Jas.

Now I am a bit clearer with what your trying to say and about the part where int marks[5][3] is the same as

const int student = 5, subject = 3; just that in the later on functions and equations, we are recommended not to use the variables student or subject because the value will be replaced by the const set.

But there are still some more doubts that I think if I don't get the whole thing, I am afraid I may not be able to understand fully and fully used it. Hence, please forgive me if I have too much questions or windy in some ways.


Questions:

From Understanding (Q3)

"passing a double into a function when you needed to be passing a pointer to a double."

I do not understand this part because I do not know what is a pointer. Hence, please explain it to me in a simpler form thanks(still learning structured programming).


From Q(3)

"1. Yes, but the functions in question are not functions called student() or subject(), as I've already explained it is in fact the copy constructor for the int datatype being used."

If I am not mistaken, you are trying to say that this copy constuctor is something like the int main() function which is already been done for you and you just need to put in the values and make use of the const to declare the int student and subject to be constant that is all. The declaration and the implementation part is already been created inside the codeblocks.

Additional Question(4)

The program that you place, I understand clearly, You place in array size, declare the arrays, then take the arrays and multiply by itself, and then take the odd subscript out and display it. But what if the program is supposed to ask the user to key in their numbers inside to this array, then we cannot use your MAX_ARRAY_SIZE[10]={2,4,6,8,10,12,14,16,18,20};

We will need to use
int bye;
int loop;
for {loop=0;loop<10;loop++)
{
cin>>bye
MAX_ARRAY_SIZE =bye;
}

then for the multiplication part, how do we use this inputs from the user and multiply?

use the same thing:

array[loop] = array[loop]*array[loop];

?

And also for the function part, if I want to use function to display would it affect any of the results?(Recommened to create a running program to show me so that I can understand better):)

Because I knew that if I begin to put these things into functions, things will get even messier and then problems will derived. Hence, I need your help to create a workable mode in function and explain to me thanks.

Questions:

From Understanding (Q3)

"passing a double into a function when you needed to be passing a pointer to a double."

I do not understand this part because I do not know what is a pointer. Hence, please explain it to me in a simpler form thanks(still learning structured programming).

You said that you were getting the following error:

(Q3)The error I had it before. They keep saying that double variable cannot be converted to double * variable(something like that)

From what you've said it sounds like you were passing something incorrectly to a function. If you post the code that gave you this error I can give you more details, but none of the code you posted contains any double or double* variables. Without seeing the code in question I can't be of any help!

From Q(3)

"1. Yes, but the functions in question are not functions called student() or subject(), as I've already explained it is in fact the copy constructor for the int datatype being used."

If I am not mistaken, you are trying to say that this copy constuctor is something like the int main() function which is already been done for you and you just need to put in the values and make use of the const to declare the int student and subject to be constant that is all. The declaration and the implementation part is already been created inside the codeblocks.

Yes, when you eventually learn about classes, you will learn about copy constructors.

Additional Question(4)

The program that you place, I understand clearly, You place in array size, declare the arrays, then take the arrays and multiply by itself, and then take the odd subscript out and display it. But what if the program is supposed to ask the user to key in their numbers inside to this array, then we cannot use your MAX_ARRAY_SIZE[10]={2,4,6,8,10,12,14,16,18,20};

We will need to use
int bye;
int loop;
for {loop=0;loop<10;loop++)
{
cin>>bye
MAX_ARRAY_SIZE =bye;
}

then for the multiplication part, how do we use this inputs from the user and multiply?

use the same thing:

array[loop] = array[loop]*array[loop];

?

And also for the function part, if I want to use function to display would it affect any of the results?(Recommened to create a running program to show me so that I can understand better):)

Because I knew that if I begin to put these things into functions, things will get even messier and then problems will derived. Hence, I need your help to create a workable mode in function and explain to me thanks.

OK, well you already said that you were able to populate the array with user input and that you were having trouble with the multiplication and displaying items at odd subscripts. I left my simple example for you to take a look at and understand. Once you've understood the code, the next part is for you to take what you've learnt from my code and apply it to your own!

The part of my code which populates the array:

// set up our array and give it some values
int array[MAX_ARRAY_SIZE] = {2,4,6,8,10,12,14,16,18,20};

is irrelevant to you. You just need to replace that line with whatever code you're using to populate your array.
If you're getting the user to manually input values, then that is fine.
Set up a loop and get the user to populate the array by entering values...No biggie!

The part you're interested in is the code that multiplies the array members and then displays the odd subscripts.
If you need it to work with a two dimensional array, you should be able to work that out quite easily!

Have a go and let us know how you get on. I may have a go at posting some more code in a bit!

Cheers for now,
Jas.

OK D.JOHN,
Take a look at the .cpp file attached at the bottom of this post (Array2DExample.cpp).

In this example, I've created a two dimensional array and created several functions which deal with populating the array, processing the array and displaying the contents of the array.

So the main part of the program does this:
1. create the array
2. pass the array to a function which prompts the user to enter some values.
3. pass the array to a function to output the entire array contents
4. pass the array to a function to manipulate each array item
5. pass the array to a function to output the array contents (the same function as step 3!)
6. pass the array to a function which outputs results for odd students odd courses.

You will notice that I am passing the array as a parameter to the functions by value.

Bearing that in mind, here are a few notes on passing by value:
Normally, passing something to a function by value will result in the original variable not being changed in the calling function.
e.g. this listing is an example of passing by value..

// example 1 - passing by value
#include<iostream>
using namespace std
void somefunction(int index)
{
    index = 25;
}

int main
{
    int index=3;
    somefunction(index);

    // what is the value of index??
    cout << index;
    return 0;
}

The cout statement at the end will output the value of index as "3" NOT "25", because the int was passed by value to somefunction().
Passing by value creates a copy of the passed in variable which the function manipulates. The copy of the variable is local to the function, it cannot be accessed outside of the function, so the changes to the value of 'index' inside 'somefunction()' do not affect the value of 'index' in 'main()'.

If we'd used a pointer or a reference to the int, then the function would be able to manipulate the value of index in main.

A little info on pointers...
I know you said you haven't learnt about pointers yet, so I won't go into too much depth, but a pointer is basically a type which holds the memory address of another variable.
So variables declared like this are pointers:

int *pInt; // pointer to an int
char *pChar; // pointer to a char

And you can assign them to point to variables like so:

int num=5;
char ch='a';

int *pInt = &num;
char *pChar = &ch;

So pInt now holds the memory address of num (&num) and pChar holds the memory address of ch (&ch).

This is a simple example of passing a pointer to a function:

// example 2 - passing a pointer
#include<iostream>
using namespace std
void somefunction(int* pIndex)
{
    *pIndex = 25; 
}

int main
{
    int index=3;
    somefunction(&index); // pass index as a pointer by passing its address (using the & operator).
    cout << index;
    return 0;
}

Now the cout will output "25".

In example 2, I could've created and passed the pointer by using:

int *pInt = &index;
somefunction(pInt);

But instead, I've just passed the address of index using the '&' operator), which is another way of passing a pointer!

somefunction(&index);

This line of code from example 2:

*pIndex = 25;

Shows how you alter the value of the variable pointed to by a pointer. The dereference operator (*) is used to dereference the pointer. So it's basically saying 'set the contents of the memory address we're pointing to, to be 25'. This will ultimately result in the value of 'index' in 'main()' being changed from 3 to 25 because pIndex points to the address of index (&index).

OK, so that's a very brief and incomplete background on pointers..I'm sure your teacher/professor will be getting around to pointers in due course.

Now back to my original point, which relates to the attached file. As already stated, typically when you pass by value, a copy of the object is created and used locally inside the function. Any changes to the variable will not be seen outside of that function. So the original variable will not be altered.

However, arrays are automatically treated as pointers, so if you pass an array by value it automatically gets treated as a pointer. Therefore functions which get passed arrays are able to modify their contents. (Unless the array has been passed in as a constant array!)

So the getInput() and processArray() functions in the attached file are passed the array and can modify its data, but the displayArray() and displayOdd() functions cannot modify the array data because the array is passed as a constant!

Hope this is of some help!
Cheers for now,
Jas.

p.s. Hope I haven't confused you too much with the pointer stuff either! Pointers are probably the most complex subject in C/C++!
The pointer stuff was just supplemental to help explain why passing the arrays by value allowed the functions to modify the contents! :P

Attachments
#include <iostream>

using namespace std;

// global constants
const int MAX_STUDENTS=5, MAX_SUBJECTS=3;

// helper functions
void getInput(int array[MAX_STUDENTS][MAX_SUBJECTS]);
void processArray(int array[MAX_STUDENTS][MAX_SUBJECTS]);
void displayArray(const int array[MAX_STUDENTS][MAX_SUBJECTS]);
void displayOdd(const int array[MAX_STUDENTS][MAX_SUBJECTS]);


int main()
{
	// initialise our array
	int marks[MAX_STUDENTS][MAX_SUBJECTS]={0};

	// get the user to input the marks
	getInput(marks);

	// Lets see what we've got so far
	cout << "The Marks entered were: " << endl;
	displayArray(marks);

	// now lets process the array and see what we get
	processArray(marks);
	cout << "After processing the marks are:" << endl;
	displayArray(marks);

	// Now let's see the results for students with odd subscripts, but only for subjects with odd subscripts...
	cout << "Odd students odd subject results:" << endl;
	displayOdd(marks);

	return 0;
}


// Get the user to populate the array
void getInput(int array[MAX_STUDENTS][MAX_SUBJECTS])
{
	for(int student=0;student<MAX_STUDENTS;++student)
	{
		cout << "Enter the marks for student " << student << ": " << endl;
		for(int subject=0;subject<MAX_SUBJECTS;++subject)
		{
			int value;
			cout << "Subject " << subject << ": ";
			cin >> value;
			array[student][subject] = value;
		}
		cout << endl;
	}
	cout << endl;
}

// Multiply each mark by itself
void processArray(int array[MAX_STUDENTS][MAX_SUBJECTS])
{
	for(int student=0;student<MAX_STUDENTS;++student)
	{
		for(int subject=0; subject<MAX_SUBJECTS; ++subject)
		{
			array[student][subject] = (array[student][subject]) * (array[student][subject]);
		}
	}
}

// Display entire contents of array
void displayArray(const int array[MAX_STUDENTS][MAX_SUBJECTS])
{
	for(int student=0;student<MAX_STUDENTS;++student)
	{
		cout << "Marks for student " << student << " :" << endl;
		for(int subject=0; subject<MAX_SUBJECTS; ++subject)
		{
			cout << "Subject " << subject << " : " << array[student][subject] << endl;
		}
		cout << endl;
	}
	cout << endl;
}

// Only display results for odd students odd subjects
void displayOdd(const int array[MAX_STUDENTS][MAX_SUBJECTS])
{
	for(int student=0;student<MAX_STUDENTS;++student)
	{
		if(student%2)
		{
			cout << "Marks for student " << student << " :" << endl;
			for(int subject=0; subject<MAX_SUBJECTS; ++subject)
			{
				if(subject%2)
					cout << "Subject " << subject << " : " << array[student][subject] << endl;
			}
			cout << endl;
		}
	}

	cout << endl;
}

I have some questions regarding the program you have created:

1:

int marks[MAX_STUDENTS][MAX_SUBJECTS]={0};

What does this array do? Isn;t this array means that the output of int marks[MAX_STUDENTS][MAX_SUBJECTS] is 0?

2:

If I am not mistaken, we are required to put a return; or return (something); at the end of the lines in or the return the data in the function back to int main() function?

But I am curious about how come the program still eun even though you didn't place the return; to it?

e.g
void getInput(int array[MAX_STUDENTS][MAX_SUBJECTS]) /*<--In this function's implemention*/

3:

In this line:

array[student][subject] = (array[student][subject]) * (array[student][subject]);

Is it a must for the array[student][subject] to be inside the brackets?

If not it will lead to an error and it cannot be multiplied by itself right?

Is there any reason why the system make it a must for that brackets?


Other than all these questions, I think I probably understand what this program does and also in fact you repeated the output twice and also the odd outputs only have 1 and 3 because 0 is not counted, 5 is outside the condition(student<MAX_STUDENT/*<---5*/). The constant is used hence all the results of MAX_STUDENT=5, MAX_SUBJECTS=3;

Afterall, this program is quite user friendly and is not too difficult after you understand alot of things:) But when it became very complicated and lots of things are required, things may get out of hand:(

int marks[MAX_STUDENTS][MAX_SUBJECTS]={0};
What does this array do? Isn;t this array means that the output of int marks[MAX_STUDENTS][MAX_SUBJECTS] is 0?

It initializes all elements in that array to 0

2:

If I am not mistaken, we are required to put a return; or return (something); at the end of the lines in or the return the data in the function back to int main() function?

But I am curious about how come the program still eun even though you didn't place the return; to it?

e.g
void getInput(int array[MAX_STUDENTS][MAX_SUBJECTS]) /*<--In this function's implemention*/

I think you don't know what a void function is. It tells the compiler
that a this void functions does not return anything back to the
place it was called at.

I see. Thanks alot and btw, my lecturer always ask us to place the return at the back of all functions for save keeping. And as for the all elements does it mean for eg. marks[3][2] =0, marks[1][0] also=0?

This article has been dead for over six months. Start a new discussion instead.