array

Correct the following code so that it correctly initializes and outputs the
elements of the array.

myList;
int myList[10];
for (int i = 1; i <= 10; i++)
cin >> myList;
for (int i = 1; i <= 10; i++)
cout << myList[i] << " ";
cout << endl;

i dont know how to run this code pls help me...thnks
Array indices go from 0 to n-1.
how to correct this code ?? pls teach me!!
closed account (2NywAqkS)
Assuming you have the rest of the code right this should work. You made a few mistakes like you forgot to put the '[i]' after myList and I don't know why you put the first line in. It seems unnecessary. Try it like this:

1
2
3
4
5
6
int myList[10];
	for (int i = 1; i <= 10; i++)
		cin >> myList[i];
	for (int i = 1; i <= 10; i++)
		cout << myList[i] << " ";
	cout << endl;


When you say you don't know how to run this code do you mean you don't have a compiler?
#include <iostream>

using namespace std;
int main ()
{
int myList[10];
for (int i = 1; i <= 10; i++)
cin >> myList[i];
for (int i = 1; i <= 10; i++)
cout << myList[i] << " ";
cout << endl;

system ("pause");
return 0;


}

thnk u very much Rowan836 .. gbu!!
That's still wrong. Array indices still go from 0 to n-1.
what do u mean?? athar??
closed account (z05DSL3A)
The first element of an array is [0] not [1].

So
1
2
3
4
5
6
7
8
9
10
11
12
const size_t arr_size =10;

int int_arr[arr_size];  // array of 10 ints

int_arr[0] = 1;      // The first element in the array
int_arr[arr_size -1] = 10;  // the last element in the array

for(size_t index = 0; index < arr_size; index++)
{
    int_arr[index] = 1;
}
closed account (2NywAqkS)
Oh yes, sorry I din't see that. Grey Wolf and Athar are very true. Also it is very bad practice to use system("pause"). A simple alternative, I find, is to
1
2
int pause;
std::cin >> pause;
Topic archived. No new replies allowed.