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);
}

Dani AI

Generated

Quick summary: is right — you just need to traverse the data and check for non-zero values. The compile errors you posted (C2109 “subscript requires array or pointer type” and C4508 about main) mean the compiler sees a or g as plain int (not an array/vector) and your program either has an invalid main or missing/extra braces. Fix those first and the logic will work.

Concrete fixes:

  • Make g (and any array you index) an actual container, e.g. std::vector<int> g(140); or int g[140];. The C2109 comes from doing g[i] when g was declared as int g;.
  • Use a proper int main() and return 0; to remove the C4508 warning.
  • Index from 0 (or consistently from 1 if you deliberately size arrays by +1), and use size_t for .size() comparisons to avoid signed/unsigned trouble.
  • Initialize accumulators (sum, temp) before use and avoid redeclaring loop variables in nested scopes. If your goal is “count columns with any non-zero value” increment a counter when you find a non-zero; if you’re building counts per column, keep g as a persistent array and increment g[col] inside the loop.

Example (counts non-zero entries in a single row):

#include <iostream>
#include <vector>

int main() {
    std::vector<int> a = {0,1,0,9,0,0,5,0,0,3};
    int nonZero = 0;
    for (int v : a) if (v != 0) ++nonZero;
    std::cout << nonZero << '\n'; // prints 4
    return 0;
}

If the real task is the nested students/examcode logic, build a clear small test harness, use std::vector<int> or std::map<std::pair<int,int>,int> for pair counts, and print intermediate values while fixing the indexing errors.

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.