Who else is reading c++ Primer 5th Edition

Pages: 12
closed account (EwCjE3v7)
okay just went across another one.

Go to pg 178 and look at exercise 5.7 ((c))

I do not have the function get_value() and cannot be sure of what the value return by that will be.
@Nathan222 His code was self explanatory.

@YellowPyrmid
Did you mean this problem?

Read a set of integers into a vector. Print the sum of each pair of adjacent elements. Change your program so that it prints the sum of the first and last elements, followed by the sum of the second and second-to-last, and so on.


I tried to keep it simple as I don't know, without re-reading, what all is covered up to that point. I also had fun and did the two requirements, but also did a third method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 
#include <vector>
#include <iostream>

int main()
{
	std::vector<int> storage;
	int input;
	std::cout << "Type in an even amount of numbers: " << std::endl;
	
	while(std::cin >> input)
	{
		storage.push_back(input);
	}
	
	std::cout << "Figuring numbers: " << std::endl;
		
	int vecSize = storage.size() - 1;
	
	// loop through vector and give sum of 1st and 2nd, then 2nd 
	// and 3rd, 3rd and 4th, and so on
	for(int j = 0; j <= vecSize; ++j)		
	{
		// poorly done as the last iteration of this loop adds 10 + 0
		// I should have checked to see if j+1 was null and if it was
		// break out of the loop
		std::cout << "Sum of " << storage[j] << " and " << storage[j+1]
		          << " is " << storage[j] + storage[j+1] << std::endl;
	}       
	
	// loop through vector and give sum of adjacent values 1 and 2, 
	// 3 and 4, 5 and 6, etc.
	for(int k = 0; k < vecSize;)
	{
		std::cout << "Sum of " << storage[k] << " and " << storage[k + 1]
		          << " is " << storage[k] + storage[k + 1] << std::endl;
		k+=2;
	}
	
	// loop through the vector and give sum of first and last, 
	// second and second to last, and so on
	for (int i = 0; i <= (vecSize/2); i++)
	{
		std::cout << "Sum of index " << storage[i]<< " and "
		     << storage[vecSize -i] << " is " << storage[i] + storage[vecSize - i] << std::endl;
	}
	
	return 0;
}


Type in an even amount of numbers: 
1 2 3 4 5 6 7 8 9 10
Figuring numbers: 
Sum of 1 and 2 is 3
Sum of 2 and 3 is 5
Sum of 3 and 4 is 7
Sum of 4 and 5 is 9
Sum of 5 and 6 is 11
Sum of 6 and 7 is 13
Sum of 7 and 8 is 15
Sum of 8 and 9 is 17
Sum of 9 and 10 is 19
Sum of 10 and 0 is 10
Sum of 1 and 2 is 3
Sum of 3 and 4 is 7
Sum of 5 and 6 is 11
Sum of 7 and 8 is 15
Sum of 9 and 10 is 19
Sum of index 1 and 10 is 11
Sum of index 2 and 9 is 11
Sum of index 3 and 8 is 11
Sum of index 4 and 7 is 11
Sum of index 5 and 6 is 11
Last edited on
closed account (EwCjE3v7)
#include <vector>
#include <iostream>

int main()
{
std::vector<int> storage;
int input;
std::cout << "Type in an even amount of numbers: " << std::endl;

while(std::cin >> input)
{
storage.push_back(input);
}

std::cout << "Figuring numbers: " << std::endl;

int vecSize = storage.size() - 1;

// loop through vector and give sum of 1st and 2nd, then 2nd
// and 3rd, 3rd and 4th, and so on
for(int j = 0; j <= vecSize; ++j)
{
// poorly done as the last iteration of this loop adds 10 + 0
// I should have checked to see if j+1 was null and if it was
// break out of the loop
std::cout << "Sum of " << storage[j] << " and " << storage[j+1]
<< " is " << storage[j] + storage[j+1] << std::endl;
}

// loop through vector and give sum of adjacent values 1 and 2,
// 3 and 4, 5 and 6, etc.
for(int k = 0; k < vecSize;)
{
std::cout << "Sum of " << storage[k] << " and " << storage[k + 1]
<< " is " << storage[k] + storage[k + 1] << std::endl;
k+=2;
}

// loop through the vector and give sum of first and last,
// second and second to last, and so on
for (int i = 0; i <= (vecSize/2); i++)
{
std::cout << "Sum of index " << storage[i]<< " and "
<< storage[vecSize -i] << " is " << storage[i] + storage[vecSize - i] << std::endl;
}

return 0;
}


How would you get out of the while loop? line 11
I added an if statement in the while loop for you:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <vector>
#include <iostream>

int main()
{
	std::vector<int> storage;
	int input;
	std::cout << "Type in an even amount of numbers: " << std::endl;
	
	while(std::cin >> input)
	{
		if(input == -999)
		{
			break;
		}else{
			storage.push_back(input);
		}
	}
	
	std::cout << "Figuring numbers: " << std::endl;
		
	int vecSize = storage.size() - 1;
	
	// loop through vector and give sum of 1st and 2nd, then 2nd 
	// and 3rd, 3rd and 4th, and so on
	for(int j = 0; j <= vecSize; ++j)		
	{
		// poorly done as the last iteration of this loop adds 10 + 0
		// I should have checked to see if j+1 was null and if it was
		// break out of the loop
		std::cout << "Sum of " << storage[j] << " and " << storage[j+1]
		          << " is " << storage[j] + storage[j+1] << std::endl;
	}       
	
	// loop through vector and give sum of adjacent values 1 and 2, 
	// 3 and 4, 5 and 6, etc.
	for(int k = 0; k < vecSize;)
	{
		std::cout << "Sum of " << storage[k] << " and " << storage[k + 1]
		          << " is " << storage[k] + storage[k + 1] << std::endl;
		k+=2;
	}
	
	// loop through the vector and give sum of first and last, 
	// second and second to last, and so on
	for (int i = 0; i <= (vecSize/2); i++)
	{
		std::cout << "Sum of index " << storage[i]<< " and "
		     << storage[vecSize -i] << " is " << storage[i] + storage[vecSize - i] << std::endl;
	}
	
	return 0;
}

Now all you have to do is, using my example earlier, 1 2 3 4 5 6 7 8 9 10 -999 and you will break out to get the answers.

Type in an even amount of numbers: 
1 2 3 4 5 6 7 8 9 10 -999
Figuring numbers: 
Sum of 1 and 2 is 3
Sum of 2 and 3 is 5
Sum of 3 and 4 is 7
Sum of 4 and 5 is 9
Sum of 5 and 6 is 11
Sum of 6 and 7 is 13
Sum of 7 and 8 is 15
Sum of 8 and 9 is 17
Sum of 9 and 10 is 19
Sum of 10 and 0 is 10
Sum of 1 and 2 is 3
Sum of 3 and 4 is 7
Sum of 5 and 6 is 11
Sum of 7 and 8 is 15
Sum of 9 and 10 is 19
Sum of index 1 and 10 is 11
Sum of index 2 and 9 is 11
Sum of index 3 and 8 is 11
Sum of index 4 and 7 is 11
Sum of index 5 and 6 is 11
closed account (EwCjE3v7)
Yea, I know that but how else do you get out of a while as this book has loads if you look at page 179

The code there how would you get out of the whille?
Well, it depends on what OS you have. Windows you would type in the 1-10, for example, hit enter, and then do CTRL+Z (if I remember right). For Linux, you would type 1-10, hit enter, then CTRL+D. These signal end of transmission, making the while false and going to the rest of the program.
closed account (EwCjE3v7)
Okay tomorrow I will try crrl d thanks
Just took time to look at 5.7(c)
1
2
3
4
if(int ival = get_value())
              cout << "ival = " << ival << endl;
          if(!ival) // ival is out of scope so you get a not declared error
              cout << "ival = 0\n";

Now, what get_value() returns isn't important. They are wanting you to fix the error. Basically, it is assuming you understand get_value() will return an int so if the value is anything non-zero it will be true else it is false.

get_value() is probably something as simple as:
1
2
3
4
5
6
7
int get_value()
{
      int val;
      std::cout << "Enter a number: ";
      std::cin >> val;
      return val;
}


Putting the int ival in the if() makes it out of scope for the if(!ival), but there are technically two ways to fix it and I did both in one example.
1
2
3
4
5
6
7
8
// move ival out of if() to make it so you can use it in more than one place
// removing the scope error you get for the second if(!ival) or
int ival = get_value(); 
if(ival){
      cout << "ival = " << ival << endl;
}else{     // changing to else would have also fixed the error if you left the scope problem alone
      cout << "ival = 0\n";
}
Last edited on
Topic archived. No new replies allowed.
Pages: 12