Thread: Excercise in book

  1. #1
    Hmm...? gin's Avatar
    Join Date
    Jun 2008
    Location
    Glasgow, Scotland
    Posts
    51

    Excercise in book

    Hi. I've been reading C++ Primer, 4th Edition lately and have been attempting the excercises as I go along. I've done most of them. I've came across one which I don't really understand:

    "Read a set of integers into a vector. Calculate and print the sum of each pair of adjacent elements in the vector. If there is an odd number, tell the user and print the value of the last element without summing it. Now 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."

    Right, I get the first sentence. But what does it mean with the second part (in bold)?

    This is what I have so far:
    Code:
    #include <iostream>
    #include <vector>
    
    int main()
    {
    	std::vector<int> Container;
    	int ContainerSize = 10;
    	int Temp = 0;
    
    	for(int i = 0; i <= ContainerSize; i++)
    	{
    		std::cout << "Element " << i << ": ";
    		std::cin >> Temp;
    		Container.push_back(Temp);
    	}
    }
    Sorry for asking it here, but there is no answer book and you guys/girls are very smart.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Suppose the vector contains: 2, 3, 4, 5, 6, 7
    Then you should print: 5 9 13
    i.e., 2+3 4+5 6+7

    Suppose the vector contains: 5, 4, 3, 2, 1
    Then you should print: 9 5 1 (odd number of elements)

    Of course, the exact format of the output is up to you.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Note that your code specifies a size of 10 (or actually 11 since your loop is set up to run 11 times). The exercise doesn't seem to want you to limit the number of entries. You can ask the user for the number of entries first, or you can tell them to input 0 or -1 or something like that to show that they are done entering numbers.

  4. #4
    Hmm...? gin's Avatar
    Join Date
    Jun 2008
    Location
    Glasgow, Scotland
    Posts
    51
    Thank you very much. And I'll ask the user for input Daved.

  5. #5
    Hmm...? gin's Avatar
    Join Date
    Jun 2008
    Location
    Glasgow, Scotland
    Posts
    51
    I'm done!

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    int main()
    {
    	std::vector<int> Container;
    	std::vector<int> FinalValues;
    	int ContainerSize;
    
    	std::cout << "Container size: ";
    	std::cin >> ContainerSize;
    
    	int Temp = 0;
    
    	for(int i = 0; i < ContainerSize; i++)
    	{
    		std::cout << "Element " << i << ": ";
    		std::cin >> Temp;
    		Container.push_back(Temp);
    	}
    	for(int i = 0; i < ContainerSize; i++)
    	{
    		//Once all values have been inserted into the vector
    		
    		if(!(i &#37; 2)) //Element number is even
    		{
    			if((i + 1) < ContainerSize)
    			{
    				FinalValues.push_back(Container[i] + Container[i + 1]);
    			}
    			else
    			{
    				FinalValues.push_back(Container[i]);
    			}
    		}
    	}
    	for(std::vector<int>::size_type i = 0; i < FinalValues.size(); i++)
    	{
    		std::cout << FinalValues[i] << " ";
    	}
    }
    Anyone give me suggestions? Pretty big but it works

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Looks good to me.

    Has your book mentioned checking for or handling input errors? If the user types a character instead of a number you'll have problems.

    One solution is to use a while loop to get input and keep asking until they input a valid number:
    Code:
    while (!(std::cin >> Temp))
    {
        // clear the fail bit
        std::cin.clear();
    
        // empty any and all bad characters from stream
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    
        // give error message and new prompt here:
        std::cout << "Invalid element, try again: ";
    }
    You'll need to #include <limits> for numeric_limits and <ios> (I think) for streamsize.

  7. #7
    Hmm...? gin's Avatar
    Join Date
    Jun 2008
    Location
    Glasgow, Scotland
    Posts
    51
    Nope, it hasn't talked about any error handling... yet. Thanks for the code, I'll have a mess about with it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. URGENT: Help wanted...in C
    By iamjimjohn in forum C Programming
    Replies: 16
    Last Post: 05-18-2007, 05:46 AM
  2. Can't display book and borrower's details.
    By grscot in forum C++ Programming
    Replies: 0
    Last Post: 05-02-2003, 10:18 AM
  3. Books on C and C++
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-28-2002, 04:18 PM
  4. C++: Reference Book, GUI, Networking & Beyond
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 11-10-2001, 08:03 PM