having problem

I wrote source code from reading but I have problem with it. So, it says, ""values is a two-dimensional array of floats with 10 rows and 20 columns. Write code that sums all the elements in the array and stores the sum in the variable total."

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
#include <iostream>
using namespace std;

int main()
{
    const int ROWSIZE = 10;
    const int COLSIZE=20;
    int i;

{ 
    int num[ROWSIZE][COLSIZE];
    cout <<"Enter input:\n";
    for ( int i = 0 ; i < ROWSIZE ; i++)
      for (int j = 0; j < COLSIZE;j++)
          cin >> num[i][j];
          cout <<"The values of the num in array's elements\n";
   for ( i=0 ; i < ROWSIZE ; i++)
  {    
       for ( j = 0; j <COLSIZE;j++)
        cout << num[i][j]<< "   ";
        cout <<endl;
   }
} 

for (int row = 0; row < num.length; row++)
{
 for (int col = 0; col < num[row].length; col++)
      {
                  sum += num[i][j];
      }
}


	system("PAUSE");
	return 0;
}
It is interesting where did you copy this loop from?

1
2
3
4
5
6
7
for (int row = 0; row < num.length; row++)
{
 for (int col = 0; col < num[row].length; col++)
      {
                  sum += num[i][j];
      }
}


it looks like this part was copied (with errors) from some program in C#.:)
Last edited on
from my friend, but he is not sure.
In C++ arrays have no member functions including such a function as length. Also variable sum was not defined and initialized.
you have random blocks of code {} in your program, that is a dead giveaway of your programming level.

tip number 1) curly brackets "{}" begin only after function definitions, loops, and if statements. they don't appear randomly in code { like this }. I mean, I'm not sure if that's even legal in C++
@ TinyTeeTree: Yes, C++ supports this, they're called Anonymous Scopes. You use them when you want an object to exist only in a limited scope but you want the code in that scope to have access to the data that is already in that function.
mistake number 1)
you have a curly bracket starting at line 10, and ending at 23.

mistake number 2)
bad indentation.
a for loop performs only one statement after it, just because something is indented after a for loop doesn't mean that it will be part of that loop, if you get confused you need to write each for loop the standard way

1
2
3
for(int I=0; I<$; ++I){
   //code
}


notice the brackets that follow the for loop? everything that goes between them will be part of the loop, if you denote brackets then the for loop captures only the next statement.

the funny thing is that the brackets in your code are correct, its the way you indented it that gives off that you don't know whats happening.

mistake number 3)
num.length and num[row].length
these things don't compile in c++. try again :)

mistake number 4)
the sum variable wasn't declared
@TinyTeeTree

mistake number 3)
num.length and num[row].length
these things don't compile in c++. try again :)


The result of your advice will be an infinite loop because he will try again and get the same result.:)
That's exactly what I meant to happen, the only way he got is to break out of the programming loop.

@Computergeek01

@ TinyTeeTree: Yes, C++ supports this, they're called Anonymous Scopes.


It is simply a compound statement. There is no such a notion as anonymous scopes in C++.
Topic archived. No new replies allowed.