I have this data

1 2 3 4 5 6 7 8 9 10
0 1 0 9 0 0 5 0 0 3

How to count rowwhich have value only?
Which the answers is only 4 column which have value?

something like this I do.

a[i]=total;
total=a[i]+temp;
myfile<<"\t"<<total<<setw(5);
}

Recommended Answers

All 3 Replies

why don't you traverse the array
and check if the value is non zero add them,

// traverse the array.
    if ( a[i] != 0)
               sum += a[i];

// after loop
cout<< sum;

this is what i understand from your description.

I done like this,,,but error. at the bold below


(109) : error C2109: subscript requires array or pointer type(110) : error C2109: subscript requires array or pointer type
(117) : warning C4508: 'main' : function should return a value; 'void' return type assumed
Error executing cl.exe.

a.exe - 2 error(s), 1 warning(s)

#include <iostream>  // std::cout
#include <fstream>
#include <iomanip>
#include <string>    // std::string
#include <vector>    // std::vector<>
#include <algorithm> //std::for each()
using namespace std; // import "std" namespace into global namespace

{
int sum, temp, total, a,i;
int count;
count=-1;
int m, n;
temp=0;
    for ( m=1; m<140; m++)
{
myfile <<"\n"<< setw(5)<<"\t"<<m; 
for (n=1; n<140; n++)
{
count=0;
for ( int i = 1 ; i < students.size (); i++ )
{
for ( int k = 1; k<students.at(i).examcode.size(); k++ )
{
if (students.at(i).examcode.at(0)==m && students.at (i).examcode.at(k)==n)
{
count++;
}			
if (students.at(i).examcode.at(0)==n && students.at (i).examcode.at(k)==m)
{
count++;
}
}
}
g[i]=count;
sum=g[i]+temp;
temp=sum;
//myfile<<g[i];
}
myfile <<"\t"<<sum<<setw(5);
sum=temp=0;
// traverse the array.
 [B] if ( a[i] != 0)
    sum += a[i];
// after loop
cout<< sum;   [/B]
		
}	}
}		
}
}

Tell me how good you are with C/C++, and one more thing what your above program does.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.