1

I've been stuck on my assignment and I have a general question (don't want my hw answered for me). If I have lines of input such as:

14 14 14 14 14 45 45
18 14 60 10 10 24 12  

and I want to compare each row(take difference of each number; 15-14, etc.) with another line of input such as:

15 15 15 15 20 20 50  

I don't wanna post my code because I'm not looking for someone to do the work for me. What I'm trying right now is to make an Array and use a for loop but I'm not sure how to store each value(separate by space), and I'm not sure how to store 5 rows into an Array.

I'm not allowed to use vectors. I'm only allowed to use iostream, sstream, and string. If anyone can provide a quick answer/hint as to how I would do this that would be great. Thank you

Justin
  • 165
  • 3
  • 14
  • your each row can be stored as an array and then you can loop through each row elements to do your comparison-based processing. Do you have any requirement to how many inputs each line should take? Also, if each row is numbers separated by a space " ", you probably will have to accept it as a string and then convert it to an array of integer and continue processing. – ha9u63a7 Nov 05 '14 at 23:01
  • Trying to figure out how I can convert it to an array of integers. I'm using a for loop and looping through each one to see if it's a space, if not then I add it to an empty string and convert the string into an int and add it to my array. Not sure if this works though – Justin Nov 05 '14 at 23:57
  • you can use `std::getline(std::cin, your_input_line)` to capture a line that will be in string. You need to tokenise that string to number and " " will be your tokeniser. I think you are probably not allowed to use tokeniser for your homework. – ha9u63a7 Nov 06 '14 at 00:02
  • yeah...I already have the getline part and I don't know how to split by space after I get the string line. Would it work if I create an empty string outside my for loop and my for loop would go through each character in the string. If it is a number,I add to string and once I hit a space I convert that string to an int and add to array. Then clear the string and start all over? Never mind. This doesn't work because I can't use the same index for the for loop and array – Justin Nov 06 '14 at 00:07
  • http://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c has a good example of how to do this. – ha9u63a7 Nov 06 '14 at 00:11
  • Thanks. It's helping me quiet a bit but can you explain what he means by "if you want to preserve the original string, just use s = s.substr(pos + delimiter.length());):............................s.erase(0, s.find(delimiter) + delimiter.length()); how does s = s...preserve the original? – Justin Nov 06 '14 at 00:32
  • All he is saying that bit code preserves your original "10 20 30 40 50" string that you received from `std::getline()`. If you don't want it, you don't care :). You can always save it to a temporary string and use it to do the substr splits./ – ha9u63a7 Nov 06 '14 at 00:35
  • last quick question...if my function returns an array would this be the correct way to write the function.. `int* arr_fun(int some_number){ int* p = new int[some_number];.......return p;}` – Justin Nov 06 '14 at 00:44

1 Answers1

1
  1. If you just want to know if the numbers are same and having nothing else to do with them.. Read each line and compare the line itself instead of comparing each number. You could use string compare.

treat "14 14 .." as one string and "15 15 .." as the other and compare them

and then the next line and so on..

  1. otherwise

create a 2d array. or an array of pointers. Split the string on 'whitespace' and store each of them as an integer in the array.

Then you could iterate over the arrays and compare them.

user19999111
  • 170
  • 1
  • 3
  • 10
  • is there a simple way to split the string? I've tried and so far what I've found use boost and vectors which I can't use. – Justin Nov 05 '14 at 23:56