functions! need help

Hi!
I am in desperate need of help ... I am new in C + + (and programming overall ...)
I have major problems with an assignment that I have been working on for a long time, without any success, and I can not find information anywhere ...
The task is to build a program that asks for two integers, prints all the numbers between the entered integers and calculates the sum (between the input rate) and the average value and print them.
procedure should also be repeated the user wants, and should be asked if he / she wants to enter two new integer (Y / N), etc. etc. etc.

it should consist of a number of functions, I have managed to make it work with the help of loops, while, for, else / if, etc., but the source code is long and convoluted (only in main ...)
How do I write it with a number of functions and function calls, etc.?

I'm really desperate and would be extremely pleased if someone could help with some advice, code snippets, etc.

(using Visual Studio Express 2010)

Sophie
A function is just a block of code that gets jumped to when you call it. It allows you to separate a larger job into smaller parts.

A simple example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void PrintSomething()  // this is a function to print "Something"
{
  cout << "Something" << endl;
}

void PrintSomethingElse()  // this is a function to print "Something Else"
{
  cout << "Something Else" << endl;
}

int main()
{
  // now let's say we want to print:
  //  Something
  //  Something Else
  //  Something Else
  //  Something

  // we could just call the above functions:
  PrintSomething();  // calls our PrintSomething function
  PrintSomethingElse();
  PrintSomethingElse();
  PrintSomething();
}


When we call Something();, the computer temporarily leaves main and jumps over to the "Something" function. It will run through the code in that function. Once that function is done, the computer returns back to the code that called it so it would jump back to that line in main).


In your case, you'd probably want to write functions to do some of the smaller tasks. Like get input from the user, or calculate the sum of all values between two given numbers, or print output to the screen, etc.

EDIT: fixed code error.
Last edited on
Here it is without functions. It's not too many lines of code.
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
int main()
{

  char choice = 'y';
  while (choice == 'y')
  {
    int low, high, sum = 0;
    float mean;

    cout << "enter two integers: ";
    cin >> low >> high;
  
    cout << "list of numbers: "
    for (int i = low; i <= high; i++)
    {
      cout << i << " ";
      sum += i;
    }

    mean = sum / (high-low+1);

    cout << endl << "sum is: " << sum << endl;
    cout << "average is: " << mean << endl;

    cout << "do it again? (y/n): "
    cin >> choice;
  }
  return 0;
}


Now if we really want to put some functions in there we could just break this down into some segments like Disch mentions. Functions are normally used to reuse code, however I don't really see any code here that is redundant and needs some re-using. Another option is breaking code into functions to make things easier to read. Something like this:

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
int print_n_sum(int i)
{
  cout << i << " ";
  return i;
}

void do_loop()
{
  int low, high, sum = 0;
  float mean;

  cout << "enter two integers: ";
  cin >> low >> high;
  
  cout << "list of numbers: "
  for (int step = low; step <= high; step++)
    sum += print_n_sum(step);

  mean = sum / (high-low+1);

  cout << endl << "sum is: " << sum << endl;
  cout << "average is: " << mean << endl;  
}


int main()
{

  char choice = 'y';
  while (choice == 'y')
  {
    do_loop();

    cout << "do it again? (y/n): "
    cin >> choice;
  }
  return 0;
}


In this case I put the meat of our main loop into its own function "do_step()" just to seperate it from that boring looping stuff as it isn't really relevant to what we are trying to calculate. It helps to reduce clutter and allows us to focus on what is really happening.

I also made print_n_sum to demonstrate how to return a value which can make something like a for loop easier to read in that "meat and potatoes" function.

mind if I see what you have so far? =)
@Disch I would just like to point out that your functions are named PrintSomething and PrintSomethingElse, not Something and SomethingElse.
@Stupebrett: you're right! Good catch. Fixed.
THANKS! I think i´ve managed it this far (well, now it's most copy-paste, some small changes (which looks awful (if (high < low) etc. don't know how to write it in another way...)
it's quite similar to the pseudo code i wrote... don't know why i have to use functions, instructions says so..
I've tried and tried and tried and tried but got completely stuck!! But now 1) it works! just have to add comments etc. and change variable names etc. etc. and 2), more important, i (think) i know how to do - have another assignmnt to do before monday (to be able to get an B/A..) which is a little bit more complicated, includes more functions and (maybe) some text strings etc. (well, its a VERY basic course.. but i'm a beginner..)

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
#include "stdafx.h"
#include <iostream>
using namespace std;

int print_n_sum(int i)
{
	cout << i << " ";
	return i;
}
void do_loop()
{
	int low, high, sum = 0;
	float mean;
	cout << "enter two integers: ";
	cin >> low >> high;
	cout << "list of numbers: ";
	if(low<high)
	{
		for (int step = low+1; step < high; step++)
		sum += print_n_sum(step);
		mean = (float) sum / (high-low-1);
	}
	if (high<low)
	{
		for (int step = high+1; step < low; step++)
		sum += print_n_sum(step);
		mean = (float) sum / (low-high-1);
	  }
	cout << endl << "sum is: " << sum << endl;
	cout << "average is: " << mean << endl;
}

int main()
{

  char choice = 'y';
  while (choice == 'y')
  {
    do_loop();

    cout << "do it again? (y/n): ";
    cin >> choice;
  }
  return 0;
}
But now 1) it works!


so you don't have any more problems with that code?
Last edited on
No, I do not think so ... at least it works on this PC in visual studio c++ (should test it in xcode too..) but if you see something I can not see (im kind of blind atm..), I would be very grateful if you want to correct me and/or comment and say what I should keep in mind ... can't reach my teacher :/
Last edited on
oh, I see. I was just wondering if you needed more help with the code since you posted it again :P

Anyways, you can leave me an email at dacster13@yahoo.com if there's anything else you require further assistance with.

I'm going to get going at the moment though, really sleepy at the moment. :P
Topic archived. No new replies allowed.