printing an array

i want to give the array all elements of 0 then print it

could you help me find what am doing wrong


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include <cmath>

using namespace std;

double const NUMB=10;


int main()
{
    int a [10];
    
    //int i;
    
    for(int i=0;i<=10;i++)
        a[i]=0;
    cout<<"I NOW IS :"<<i<<;
    
    return 0;
    
}


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
#include <iostream>
//#include <cstdlib>
//#include <fstream>
//#include <string>
//#include <cmath>

using namespace std;

//double const NUMB=10;


int main()
{
    int a [10];
    
    //int i;
    
    for(int i=0;i<10;i++)   //use i<10 only
	{
        a[i]=0;
		cout<<"I NOW IS :"<<i<<endl;
	}
	for(int i=0;i<10;i++)  //printing array
		cout<<a[i]<<"	";
    
    return 0;
    
}
Last edited on
You need only one header <iostream> for your program.

To initialize an integer array with zeroes it is enough to write

int a[10] = {};

In your loop you are trying access a memory beyond the array. The array has indexes in the range 0 - 9 . So the loop shall be set the following way

for ( int i = 0; i < 10; i++ )

Variable i is a local variable for the loop. So it will be destroyed after the control exit the loop. So for this statement

cout<<"I NOW IS :"<<i<<;

the compiler shall issue a compilation error because i is not declared in this block.

Also it is not clear what is the constant double const NUMB=10; defined for?
Last edited on
thank you so much that makes much more sense one array set them to 0 and the other to print them

i used the const and the other headers for practice just in case i need them
Topic archived. No new replies allowed.