getting a problem with return array value inside for loop...

I want to return array value inside the for loop, but the output do not have any value, can anyone help me to sovle the problem???

NOTE: i do not want to use void main()...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream.h>

float main ()
{
	float colsum[3]={0}, num[2][3]={{2,4,6},{8,10,12}};

	for (int i=0; i<2; i++)
	{
		for (int j=0; j<3; j++)
			colsum[i] += num[i][j];
	}

	for (i=0; i<2; i++)
		return colsum[i];
}


first, main doesn't return a float. The declaration of main is incorrect.

Second, the return inside the for loop makes no sense. main is the entry point of your program. The moment that the return statement executes your program ends! Do you want to print each value?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
   float colsum[3]={0}, num[2][3]={{2,4,6},{8,10,12}};

   for (int i=0; i<2; i++)
   {
	for (int j=0; j<3; j++)
       {
	   colsum[i] += num[i][j];
       }
   }


   for(int i = 0; i < 2; i++) // not sure why colsum was sized with a 3
   {
      std::cout << "colsum[" << i << "] = " << colsum[i] << std::endl;
   }

   return 0;
}


Also, use <iostream> (not <iostream.h>).
Last edited on
I have solve the problem, but i want to use it as a function and pass to the main program, for example:

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream.h>

float Column_SUM (float);

int i, j, num[3][5];

void main ()
{	
    for (i=0; i<5; i++)
    {
        for (j=0; j<3; j++)
        {
            cout << "Enter [" << i << "][" << j << "] numbers : ";
            cin >> num[i][j];
        }
            cout << "\n";
    }

    for (i=0; i<5; i++)
    {
        for (j=0; j<3; j++)
            cout << num[i][j] << "\t";
        cout << endl;
    }

    cout << endl;

    for (j=0; j<3; j++)
    {
        cout << "Number\t-->";

        for (i=0; i<5; i++)
        {
            // display the Column_SUM in here
            cout << "\t" << num[i][j];
        }
        cout << "\t=" << "\tSum"<< "\n";
    }
}

float Column_SUM (float column_sum)
{
    float colsum[3]={0};

    for (i=0; i<3; i++)
    {
        for (j=0; j<5; j++)
            colsum[i] += num[i][j];
    }

    for (i=0; i<3; i++)
        cout << colsum[i];

    return 0;
}


I want to add up each row value and display in the "Sum", how to pass the function to the main program???

When i put like this: it's infinite loop!!!

1
2
3
4
5
6
7
8
9
10
11
12
for (j=0; j<3; j++)
{
    float abc[3];	

    cout << "Number\t-->";

    for (i=0; i<5; i++)
    {
        // display the Column_SUM in here
        cout << "\t" << num[i][j] << Column_SUM (abc[i]);
    }
}
i is declared globally. main uses it in its for loop and then Column_SUM reuses it in its for loop.

declare i inside main() and then again inside Column_SUM.

I dont get you...
still confuse...
This should help.
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
using namespace std;
void Column_SUM ();

int i, j, num[3][5];

int main ()
{	
    for (i=0; i<2; i++)
    {
        for (j=0; j<4; j++)
        {
            cout << "Enter [" << i << "][" << j << "] numbers : ";
            cin >> num[i][j];
        }
            cout << "\n";
    }
	
    cout << endl;

    Column_SUM(); // you FAILED to use the function at all,
	return 0;                        
}
/* if you want to use data stored in variable(s) you can declare the function 

as int Column_SUM(int input1, int input2) and assuming we have previously 

declared and stored values in variables int number1, and int number2, we can 

call the function in the manner Column_SUM(number1,number2); now 
 
remember that you use the variables input1 and input2 within your function, 

of course you might want to make it return a value of some sort, say...return input1*input2; 
 and if you use it as int mytimesd=Column_SUM(number1,number2);  the value of input1 * input2 will 

be stored in mytimesd, of course it will be the same as number1*number2, 

and after the function is finished, the temporary variables input1 and input2 

are destroyed, so don't expect to use them outside of the function.
SORRY SO LONG
*/

void Column_SUM ()
{
    int colsum[3]={0};

    for (i=0; i<2; i++)
    {
        for (j=0; j<4; j++)
            colsum[i] += num[i][j];
    }

    for (int d=0; d<3; d++)
        cout << colsum[d] << endl;

    return;
}



You might note that arrays start at 0!! meaning when you declare num[3] there are 3 elements, namely 0, 1, and 2 so we use for(int i=0; i<2, i++)
this will do the statements in the {} things one time, then increment it.. so the first iteration it starts at i =0, then we use cout << num[i]; and it will start at element 0 (which is the first element...) and proceed to element 2 which is the last element, the third one...

basically, when you declare an array, you can use the number of elements you want, but when accessing them, it starts at 0, so your index is -1

I'm not real sure, but i think when you use ++i instead of i++ it will increment i before the first iteration.

also, using some different variable names might help, for clarification.
Last edited on

I'm not real sure, but i think when you use ++i instead of i++ it will increment i before the first iteration.


No.
Topic archived. No new replies allowed.