3

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.

2
  • What type do x and y have? Unless something fancy is going on behind the scenes, each iteration of the input loop overwrites the previously entered values. Commented Oct 17, 2013 at 6:57
  • sorry they are both integer type Commented Oct 17, 2013 at 7:00

4 Answers 4

5

You are confusing a lot of things here.

  1. In your for-loop you are overwriting the value stored in x and y at each iteration of the loop.
  2. int numbers[4][4] creates a two-dimensional array containing a total of 16 elements. What you want is int numbers[4][2].
  3. Your array initialization is incomplete, because 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.

Sign up to request clarification or add additional context in comments.

Comments

4

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]; } 

1 Comment

The difference is the for-loop boundaries. For-loops going through arrays should almost always start at 0, not 1, and continue while less than array size, not less than array size + 1. Do that and this problem goes away.
2

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.

1 Comment

thanks for your snippet. I got an idea for what to do already
2

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; } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.