Reputation: 679
I am learning about arrays , what I wanted to try is first let the user enter x,y values 4 times e.g
first time
x = 1
y = 3
second time
x = 2
y = 3
third time
x = 3
y = 1
fourth time
x = 1
y = 3
. and then store the value that the user had key in 4 times inside a array and print them out but I getting some weird outputs.
my output
10001711642800 <-- some weird output
expected output
1,3
2,3
3,1
1,3
code(not working)
int x;
int y;
//request the user to enter x and y value 4 times.
for (int i=1; i<5; i++) {
cout << i << "Please enter x-cord." << endl;
cin >> x;
cout <<i << "Please enter y-cord." << endl;
cin >> y;
}
//intitalize the array size and store the x,y values
int numbers[4][4] = {
x, y
};
//loop through 4 times to print the values.
for (int i = 0; i<5; i++) {
cout << numbers[i][i];
}
I know it can be done with vectors but now I am trying with arrays because I am weak in using arrays.
Upvotes: 3
Views: 62593
Reputation: 33004
You're not storing the input received from the user in any array. They're overwritten again and again in the loop. Store them in an array and then display it.
//intitalize the array size and store the x,y values
int numbers[4][4] = {
x, y
};
This is not required. Since you're going to overwrite the contents of the array you needn't initialize them with some random variables. In fact the variables x
and y
are not at all required.
#include <iostream>
int main(int argc, char *argv[])
{
// you need 4 arrays of 2 numbers, not 4x4 but 4x2
int numbers[4][2] = { { } }; // initialize all of them to 0
for (size_t i = 0; i < 4; ++i)
{
std::cout << "x = ";
std::cin >> numbers[i][0]; // store the input directly in the array
std::cout << "y = "; // instead of using a dummy
std::cin >> numbers[i][1];
}
// display array contents
for (size_t i = 0; i < 4; ++i)
{
std::cout << numbers[i][0] << ", " << numbers[i][1] << std::endl;
}
}
Upvotes: 2
Reputation: 1877
First, you have to declare the variable you need to fill;
// A new data type which holds two integers, x and y
struct xy_pair {
int x;
int y;
};
// A new array of four xy_pair structs
struct xy_pair numbers[4];
Then, you can begin filling it.
//request the user to enter x and y value 4 times.
for (int i=1; i<5; i++) {
cout << i << "Please enter x-cord." << endl;
cin >> numbers[i].x;
cout <<i << "Please enter y-cord." << endl;
cin >> numbers[i].y;
}
Then, you can print it!
//loop through 4 times to print the values.
for (int i = 0; i<5; i++) {
cout << "X is " << numbers[i].x << " and Y is " << numbers[i].y << endl;
}
PS! I haven't run the code myself, let me know if it doesn't work.
Upvotes: 2
Reputation: 76876
You are confusing a lot of things here.
for
-loop you are overwriting the value stored in x
and y
at each iteration of the loop.int numbers[4][4]
creates a two-dimensional array containing a total of 16 elements. What you want is int numbers[4][2]
.x
and y
only contain the last two values the user has entered, not all 8.To fix this, you should create the array before the for
-loop and then store the values the user enters directly into the array.
Upvotes: 5
Reputation: 6805
use this code:
int numbers[4][2];
for(int i=0;i<4;i++)
{
cout<<i<<": Please enter x-cord."<<endl;
cin>>numbers[i][0];
cout<<i<<": Please enter y-cord."<<endl;
cin>>numbers[i][1];
}
for (int i = 0; i<4; i++)
{
cout << numbers[i][0]<<" "<<numbers[i][1];
}
Upvotes: 4