By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 1,941 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

Problem with Function

P: 40
please help me...I can't get the correct output...My professor suck's because she's didn't discuss thoroughly the in's and out's of C programming..please help...Here's the problem: I have to write a program using a function that when you input a number, it will add the number from 1 to the number that was input, also you will show how the numbers where squared.

Desired output:
Enter a number: 5
1+4+9+16+25
The sum is: 55

My ouput:
Enter a number: 5
1+4+9+16+25
The sum is: 5869

Thank you very much in advance..here's my code, there's definitely wrong with this

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. int funct(int x);
  3. int functi(int y);
  4. int main()
  5. {
  6.    int c, d, num;
  7.    clrscr();
  8.    printf("\nEnter a number: ");
  9.    scanf("%d",&num);
  10.    for(c=1;c<=num;c++){
  11.       printf("%d+",funct(c));
  12.    }
  13.    printf("\nThe sum is: %d",functi(d));
  14.    getch();
  15. }
  16.  
  17. int funct(int x)
  18. {
  19.    int a;
  20.    a=x*x;
  21.    return a;
  22. }
  23. int functi(int y)
  24. {
  25.    int b;
  26.    b=y+b;
  27.    return b;
  28. }
Feb 23 '07 #1
Share this Question
Share on Google+
16 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
please help me...I can't get the correct output...My professor suck's because she's didn't discuss thoroughly the in's and out's of C programming..please help...Here's the problem: I have to write a program using a function that when you input a number, it will add the number from 1 to the number that was input, also you will show how the numbers where squared.

Desired output:
Enter a number: 5
1+4+9+16+25
The sum is: 55

My ouput:
Enter a number: 5
1+4+9+16+25
The sum is: 5869

Thank you very much in advance..here's my code, there's definitely wrong with this

#include <stdio.h>
int funct(int x);
int functi(int y);
int main()
{
int c, d, num;
clrscr();
printf("\nEnter a number: ");
scanf("%d",&num);
for(c=1;c<=num;c++){
printf("%d+",funct(c));
}
printf("\nThe sum is: %d",functi(d));
getch();
}
int funct(int x)
{
int a;
a=x*x;
return a;
}
int functi(int y)
{
int b;
b=y+b;
return b;
}
You never initialize d. When you declare it, the compiler gives it memory space - and d becomes x04a3d (some arbitrary slot of memory). However, it does not initialize this with a value. Some program you were using before used that memory spot x04a3d, and there is a random "garbage" value in it that d now has. That is the value you are passing to functi() (by the way, it is considered bad practice to name two functions and only have them differ by one character... it will also get very confusing for you when you get into longer programs - I would recommend mult(int x) and addr(int y) for them... somewhat indicative of what they do... But you need to set values for 'd', the numbers you want to add.
Feb 23 '07 #2

sicarie
Expert Mod 2.5K+
P: 4,677
And since you got it to compile, I'm guessing that's not your code copied directly from your program - the compiler would have yelled about a few things in there - you're using getch() (the function that annoys me the most out of any language and should be banned forever), but don't declare conio, and you declare main as int, but don't return anything at the end. Just a heads up if you try to copy that code...
Feb 23 '07 #3

DeMan
100+
P: 1,806
Perhaps, in the OP's opinion, a compiler error is not the right output? ;)
Feb 23 '07 #4

P: 40
You never initialize d. When you declare it, the compiler gives it memory space - and d becomes x04a3d (some arbitrary slot of memory). However, it does not initialize this with a value. Some program you were using before used that memory spot x04a3d, and there is a random "garbage" value in it that d now has. That is the value you are passing to functi() (by the way, it is considered bad practice to name two functions and only have them differ by one character... it will also get very confusing for you when you get into longer programs - I would recommend mult(int x) and addr(int y) for them... somewhat indicative of what they do... But you need to set values for 'd', the numbers you want to add.
so, i have to add another for loop to set a value for d? or am i going to edit function with regard to 'd'? am i going to delete the last function?
Feb 23 '07 #5

P: 40
And since you got it to compile, I'm guessing that's not your code copied directly from your program - the compiler would have yelled about a few things in there - you're using getch() (the function that annoys me the most out of any language and should be banned forever), but don't declare conio, and you declare main as int, but don't return anything at the end. Just a heads up if you try to copy that code...
what do you mean by that? Will i disregard the getch() function? what function am i going to use in lieu of the getch()? is it obsolete or a bad practice to use getch?
Feb 23 '07 #6

DeMan
100+
P: 1,806
The point that was being made is that getch() comes from a library that you haven't included......(abnd some people have strong feelings about that library, as you will soon learn....)
Feb 23 '07 #7

P: 40
The point that was being made is that getch() comes from a library that you haven't included......(abnd some people have strong feelings about that library, as you will soon learn....)
but if you don't include getch() there is no display in the screen?
Feb 23 '07 #8

Ganon11
Expert 2.5K+
P: 3,652
There are other, library independent methods you can use to keep the output on the screen. Not everyone has conio.h, but getch() is only defined in conio - a much more universal solution would be to use a scanf call.

You shouldn't need two functions to do this - you should have a variable that will keep the sum of the numbers as you call the function in the loop.
Feb 23 '07 #9

P: 40
There are other, library independent methods you can use to keep the output on the screen. Not everyone has conio.h, but getch() is only defined in conio - a much more universal solution would be to use a scanf call.

You shouldn't need two functions to do this - you should have a variable that will keep the sum of the numbers as you call the function in the loop.
int funct(int x)
{
int a;
a=x*x;
return a;
}
int functi(int y)
{
int b;
b=y+b;
return b;
}

so i'll write the code like this...
int funct(int x)
{
int a, b=0;
a=x*x;
b=b+a;
return b;
}

is that what you mean? sorry for the question...i just want to understand the program...thanx =)
Feb 23 '07 #10

sicarie
Expert Mod 2.5K+
P: 4,677
int funct(int x)
{
int a;
a=x*x;
return a;
}
int functi(int y)
{
int b;
b=y+b;
return b;
}

so i'll write the code like this...
int funct(int x)
{
int a, b=0;
a=x*x;
b=b+a;
return b;
}

is that what you mean? sorry for the question...i just want to understand the program...thanx =)
DeMan - he said he was getting a value printed out, so I was kind of assuming he was able to compile and run. Then I added that second part just in case.

I personally consider getch() bad practice because of what Ganon11 said - many people like using it, I was just going on my usual tirade against it (and I can be ignored if you feel a certain fondness for it - it's not "wrong", but you will have to do more checking if you write larger distributed programs with it).

In your updated code, I believe you have your logic error fixed - but did you need to print out the squared values to add together - if so, you will need one more printf call - but that function looks right (at first glance).
Feb 23 '07 #11

sicarie
Expert Mod 2.5K+
P: 4,677
The point that was being made is that getch() comes from a library that you haven't included......(abnd some people have strong feelings about that library, as you will soon learn....)
By some people, he means me. As this is just another thread I am hijacking to speak out against non-standard libraries!

::ties my tie around my head, twirls mouse over head by its cord::
Feb 23 '07 #12

Banfa
Expert Mod 5K+
P: 8,916
Expand|Select|Wrap|Line Numbers
  1. int functi(int y)
  2. {
  3.    int b;
  4.    b=y+b;
  5.    return b;
  6. }
  7.  
This function will not work as intended because b is on the stack. Since it is on the stack it is not implicitly initialised to anything and you do not explicitly initialise it to anything so it will have a random value (from whatever last used that piece of memory).

Since b starts with a random value and all you do is increment it by y it will end with a random value.
Feb 23 '07 #13

P: 40
Expand|Select|Wrap|Line Numbers
  1. int functi(int y)
  2. {
  3.    int b;
  4.    b=y+b;
  5.    return b;
  6. }
  7.  
This function will not work as intended because b is on the stack. Since it is on the stack it is not implicitly initialised to anything and you do not explicitly initialise it to anything so it will have a random value (from whatever last used that piece of memory).

Since b starts with a random value and all you do is increment it by y it will end with a random value.

hello banfa,
what do you mean by stack?
But on the other hand what's the difference between this two code?

int sum=0, x;
for(x=1;x<=5;x++){
sum+=x;
}
printf("%d",sum);

compare to this-->

int functi(int y)
{
int b;
b=y+b;
return b;
}

or did I forgot to initialize b to zero(0)?
Feb 25 '07 #14

P: 1
help me, i have a program that should prompt a user for 3 sides of a triangle and i should use functions to determine if the values form a right-angled triangle, isosceles, scalene, or an equilateral triangle....im having some difficulties as in what i should do in the functions.
Feb 25 '07 #15

Banfa
Expert Mod 5K+
P: 8,916
hello banfa,
what do you mean by stack?
Actually that's a minor error on my part, I sould have said it's a automatic (auto <- this is a C/C++ keyword you can look it up) variable, it is just that the majority of systems put auto variables onto their stack.

The point is that the life time of an auto variable is only the run time of the function, the variable b in you function gets re-created every time the function is called.

Expand|Select|Wrap|Line Numbers
  1. int sum=0, x;
  2.        for(x=1;x<=5;x++){
  3.               sum+=x;
  4. }
  5. printf("%d",sum);
  6.  
sum is an auto variable, however it is in scope for the entire length of the calculation however in
Expand|Select|Wrap|Line Numbers
  1. int functi(int y)
  2. {
  3.    int b;
  4.    b=y+b;
  5.    return b;
  6. }
  7.  
b (which you didn't initialise however while that is an error it is not the point) is an auto variable that is only in-scope and exists when the function is running. When ever b goes out of scope when the function is not running the memory used by b is returned to the system and can be used by someone else.
Feb 25 '07 #16

sicarie
Expert Mod 2.5K+
P: 4,677
help me, i have a program that should prompt a user for 3 sides of a triangle and i should use functions to determine if the values form a right-angled triangle, isosceles, scalene, or an equilateral triangle....im having some difficulties as in what i should do in the functions.
Sure, just start up a thread with some name that describes your problem, and we'll be more than happy to help. (you posting your question is called a thread hijack, and it does not allow us to answer the OP's question, or let people looking for answers to your question find the answer)
Feb 25 '07 #17

Post your reply

Sign in to post your reply or Sign up for a free account.