I am learn about using arrays and i need help

#include <cstdio>
#include "simpio.h"
#include "strlib.h"
#include <iostream>
using namespace std; 

#define Max 6

int main()
{
	int power2[Max], i, n;
	
	for (i=1; i<=32; i++)
	{
		power2[i] = i;
		i = i*2;
	}
	for(n = 0; n<=Max; n++)
	{
	printf("%d	",power2[n]);
	}
	system("pause");
}

The outcome is not write. any help

-858993460        1       -858993460        3        -858993460            -858993460             -858993460           Press any key to continue...

Recommended Answers

All 3 Replies

>>The outcome is not write.
write means to scribble something on a piece of paper or in a file. What you want is the word right which means correct.

1) All arrays are numbered from 0 to the number of elements in the array. In your example the array has elements numbered 0, 1, 2, 3, 4, and 5. Count them on your fingures -- that is 6 elements.

2) why does i get incrementd from 1 to 32 when the array only has 6 elements? After the program will just scribble all over memory and what happens can be any one's guess (called undefined behavior).

Take another look at those two loops, count them on paper or in your head so that you can see in your mind what is wrong with them.

Thanks... and sorry i cant say all of it was because i was new to arrays...

Are you programming in C or in C++?

#include <cstdio>
#include <iostream>
using namespace std;

All these belongs to the C++ language.

int main( void ) { /* needs to return 0; on success */ }
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.