0

My input format the number of types of items, followed by N (N - no.of items) lines each representing a pair of integers in the format L B where L is the length of the item and B is the breadth of the item.

Sample input format:
2
1 1
5 4

My question is how send all lengths into one array and all breadths into one array with out using vectors in C++. I know these kind of questions were already asked by people but I couldn't figure out how to send them to two different arrays.

my program should run in a sandbox. Sandbox automatically gives input to the program. So I have to write my program in a way that takes the above input format

3
  • I have not understood what these numbers 2 1 1 5 4 mean. Commented Nov 18, 2013 at 17:20
  • Would help to know how you get that input. Is it a txt file? Commented Nov 18, 2013 at 17:21
  • Its not a text file. my program should run in a sandbox. Sandbox automatically gives input to the program. So I have to write my program in a way that takes the above input format Commented Nov 18, 2013 at 17:25

1 Answer 1

1
cin >> n;
int *l,*b;
l = new int[n];
b = new int[n];

for(int i=0; i<n; ++i) {
  cin >> l[i] >> b[i];
}
Sign up to request clarification or add additional context in comments.

5 Comments

I will enter the whole string 1 1 at a time with a space in the middle. Do I not need to split the string by space
Yes, a space is needed to seperate the two integers. Also you can enter your numbers with any number of spaces or newlines, just the order matters.
The length of the array has to be a constant known at compile time, so you can't declare them like that. Use std::vector<int> l(n), b(n). Since vectors return references, the subsequent lines in the loop will still work.
@HeywoodFloyd Can you tell me other way without vectors. Thanks
user3005614, the edited code above will do it. However, you'll need to free up the l and b arrays when you're done with them, or you'll have a memory leak.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.