-2

i need help din adding the elements of the 2 column. the out of the 2nd column should be 2(bcos 1+1). add the values of each cell(sum =2). how to add the elements of a column. the loop should move down the column and add the values

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cstdlib>

using namespace std;

int main()
{
  char text[6][6];
    ifstream stream1("c:\\cpptestdata.txt");

    if(!stream1)
    {
        cout<<"Cannot read file\n";

    }

    while(!stream1.eof())
    {
        for(int i=0; i<6; i++)
        {
            for(int j=0; j<6; j++)
            {
                stream1>>text[i][j];
            }

        }

    }
//checking if it has been correctly inserted.
for(int i=0; i<6; i++)
    {
        for(int j=0; j<6; j++)
        {
            cout<<text[i][j]<<"\t";

        }
        cout<<"\n";

    }
 cout<<"first two rows:"<<endl;
 int i,j;
 for (i=0; i<2; i++){
 for (j=0; j<6; j++){
            std::cout<<text[i][j]<<"\t"<<' ';

            }cout<<endl;
}
cout<<"find immediate neighbours of A:"<<endl;
char largest=text[1][1];
for(i=0; i<6; i++){
    for(j=1; j<2; j++){
        if(text[i][j]>largest)
             cout<<text[i][0]<<"N"<<"\t";
        else
        cout<<"0";


}cout<<endl;
}
        cout <<" finding k neighbours for A : "<<endl;

        for (i=1; i<6; i++){
        int max = text[1][1]-'0';
     for(j = 1; j<2; j++){
            if(max < (text[i][j]-'0')){
                max = text[i][j]-'0';
                cout<<max;
            }
            else
                cout <<"xx";
        }cout<<endl;
}


       return 0;
    }
SVO
  • 11
  • 1
  • 3
  • Your Question is not very clear, improve it. If possible, also show us an example and what you expect to do ( what your expected answer is ) and what you are getting with your current code. – Arun A S Feb 23 '15 at 10:39
  • Did you try to implement it in your code ? What is the relation between the question and the posted code? – Jackzz Feb 23 '15 at 11:14

2 Answers2

1

In general, if you want to access column c you write something like this:

for (int i = 0; i < 6; ++i) {
    // access column c with text[i][c]
}
Paul Evans
  • 27,315
  • 3
  • 37
  • 54
  • @ArunA.S Both i++ and ++i increment the value of i by 1. See http://stackoverflow.com/questions/24886/is-there-a-performance-difference-between-i-and-i-in-c and http://stackoverflow.com/questions/24853/what-is-the-difference-between-i-and-i – tourniquet_grab Feb 23 '15 at 13:13
0

The following code is written to add the elements of the second column of your array, assuming the following things from your code:

  1. It is a 6x6 array.
  2. Instead of integer array, you are using a character array. (I didn't understand the need for this).

    cout << "sum of elements\n";
    int sum = 0;
    for(i=0;i<6;i++)
    {
    
      sum = sum + (text[i][2] - '0'); //Convert char to int and add
      cout <<" SUM = "<<sum<<"\n";
    }
    

If no special reasons, define the array as an int array itself. Then you could directly add the values.

for(i=0;i<6;i++)
{

  sum = sum + text[i][2]; 
  cout <<" SUM = "<<sum<<"\n";
}
Jackzz
  • 1,417
  • 4
  • 24
  • 53