Thread: **Easy ** Please help!

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    2

    **Easy ** Please help!

    The Exercise
    Consider the following code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      int low;
      int high;
    
      cout << "Enter a value:";
      cin >> low; 
      cout << "Enter a value:";
      cin >> high; 
    
      cout << "Thank you for the input!" << endl;
    
      int sum=0;
      for(int i=low; i<=high; i++)
        sum+=i*i;
    
      cout << "The sum from i=" << low << " to " << high << " of i^2   is :"
           << sum << endl;
    
      return 0;
    }

    Your task is to rewrite this program so that it performs exactly the same task but has a main
    function that must look exactly like the following:


    Code:
    int main()
    {
      int low=get_value();
      int high=get_value();
    
      cout << "Thank you for the input!" << endl;
    
      cout << "the sum from i=" << low << " to " << high << " of i^2   is :"
           << sumi2(low,high) << endl;
    
      return 0;
    }


    Could someone please help me with this?
    I've been trying for hours, im new to c++ and just need some help.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    What is this? homework evening?

    Well you should read up on functions...read and understand what is said here:
    Cprogramming.com Tutorial: Functions
    and come back if you still have trouble.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    2
    I tried.. i just cant get it.
    Code:
    #include <iostream>
    using namespace std;
    
    
    
    void EnterValue();
    void Lowvalue();
    void Highvalue();
    int get_value();
    
    
    int main()
    {
      int low=get_value();
      int high=get_value();
    
      cout << "Thank you for the input!" << endl;
    
      cout << "the sum from i=" << low << " to " << high << " of i^2   is :"
           << sumi2(low,high) << endl;
    
      return 0;
    }
    
    void EnterValue()
    {
    	int k,l;
    	cout << "Enter Value:";
    	cin >> k;
    	cout << "Enter Value:";
    	cin >> l;
    }
    
    void Lowvalue()
    {
    	int sumi2=0;
    	for(int i=low; i<=high; i++)
    		sum+=i*i;
    	cout << "The sum from i=" << low << " to " << high << " of i^2  is :" << sumi2 << endl;
    }
    
    int get_value()
    {
    	lowvalue();
    }

    what am i doing wrong?

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Ok start from the beginning with the assignment, what exactly has changed betwean the 2 codes you have been given? Can you identify what functions you need to add by looking at these changes? Can you identify what part of the code those functions should replace?

    Compare the code line by line and not down difference in functions and where that function appears and so on.

    Hope this can help you.

Popular pages Recent additions subscribe to a feed