I am trying to set all the values within my array to zero, for some reason the last row of the array will not set to zero.
Any ideas?
#include <iostream>
int main ()
{
int data[200][4]={};
for(int i =1;i<=200;i++){std::cout<<"row number is..."<<i<<"\t"<<data[i][1]<<"\t"<<data[i][2]<<"\t"<<data[i][3]<<"\t"<<data[i][4]<<std::endl;}
return 0;
}
#include <iostream>
int main ()
{
int data[200][4]={{0}};
for(int i =0;i<200;i++)
std::cout<<"row number is..."<<i<<"\t"<<data[i][0]<<"\t"<<data[i][1]<<"\t"<<data[i][2]<<"\t"<<data[i][3]<<std::endl;
return 0;
}
Array starts from 0 not from 1. So you go set values from i = 0 to i = 199.
But now you live data[0][0] without value, and try to add value to data[200][0] that does not exist
#include <iostream>
usingnamespace std;
int main()
{
int v[10];
/*init v seting 0 in each position of array*/
memset(v, 0, sizeof(v)); //clear buffer
/*Display content of v*/
for(int i=0; i<(sizeof(v)/sizeof(int)); i++)
cout<<v[i]<<endl;
/*w8 for user to press key to exit*/
cin.get();
cin.get();
return 0;
}
@gtkano: -1 for suggesting an unsafe function. Note that a call to "memset( )" after a declaration context is not initialisation. The better alternative here is "std::vector":
std::vector<int> array(200, 0); // Add 200 ints with the value of 0