currently I have a question at hand that I don't understand due to a few things. Here is the question:
Que. 1. Given the following program, show the values of the variables a, b, c, x, y, z in the main function after each function call to FindSum. Also, show the values of a, b, c in FindSum immediately after executing each function call to FindSum.
And here is the code:
#include <stdio.h>
/* function prototype declaration for FindSum */
void FindSum(int, int, int *);
int main(void)
{
int a=2, b=5, c=1, x=3, y=4, z=7;
FindSum (a, b, &c); /* a first call to FindSum */
printf(“first call in main %d %d %d %d %d %d \n”, a, b, c, x, y, z);
FindSum (x, y, &z); /* a second call to FindSum */
printf(“second call in main %d %d %d %d %d %d \n”, a, b, c, x, y, z);
return 0;
}
/* definition of FindSum */
void FindSum (int a, int b, int *c)
{
a += (b * 2);
b += (b * 2);
*c += (b * 2);
printf(“in FindSum: %d %d %d \n”, a, b, *c);
My questions:
1) Which one is the main function?
2) Is the problem asking me to finish up the code so that it produces the desired result?
3) Also, show the values of a, b, c in FindSum immediately after executing each function call to FindSum" Where is the function call to findsum
I'm currently in a C programming class where the professor doesn't explain well/english is subpar. Due to this, I am struggling slightly on these issues. Was hoping someone could shed me light
1) Which one is the main function?
The one named main.
2) Is the problem asking me to finish up the code so that it produces the desired result?
No, it's asking you to act like the computer and "run the program" on paper and say what it would output.
3) Also, "show the values of a, b, c in FindSum immediately after executing each function call to FindSum" Where is the function call to findsum
They're marked with comments in the code. Search for "call to FindSum".
It's important to understand the basic structure of a program. Including how functions work and the difference between pass by value and pass by reference. Understanding this is key towards answering these questions. The "int main(void)" line is the main function declaration. The left and right brackets define it's scope. Note the values of the variables a,b,c,x,y,&z at the beginning. Write them on a piece of paper. The FindSum function takes in two parameters and outputs one parameter. Perform the calculation and write down your results. This answers the first part of the question. Then step through line of code and write down the results after each call to FindSum. This is not asking for you to modify the code, but simply execute what the computer would compute.
Related
I am confused when to use 'return' while using recursive function calls.
I am trying to find the "GCD (Greatest common divisor)" of two numbers. What I actually thought would work is:
include <stdio.h>
int gcd (int a, int b);
int main ()
{
int a, b;
printf ("Enter two numbers \n");
scanf ("%d %d", &a, &b);
printf ("GCD for numbers %d and %d is %d\n", a, b, gcd(a,b));
return (0);
}
int gcd (int a, int b)
{
while (a!=b)
{
if (a > b)
gcd(a-b,b);
else if (b > a)
gcd(a,b-a);
}
return (a);
}
But the above code continuously accepts numbers from terminal and fails to run the code.
However, when I replace the function definition as follows the code works as expected returning the right values.
int gcd (int a, int b)
{
while (a!=b)
{
if (a > b)
return gcd(a-b,b);
else if (b > a)
return gcd(a,b-a);
}
return (a);
}
As you see the only change is addition of 'return' before the recursive function call. Why is return required there considering in both the cases I am calling the gcd(arg1, arg2) function?
Why is return required there considering in both the cases I am calling the gcd(arg1, arg2) function?
For the same reason that it is required any other time you call a function and wish to return the value that was returned by that function call; because calling it only calls it, and does nothing else with the resulting value.
I am confused when to use 'return' while using recursive function calls.
Use return for a recursive call, whenever you would use return for any other function call - i.e.: when, and because, that call returns the value you wish to return this time around.
Imagine that we have
#include "magic.h" /* defines gcd2(), which computes GCD in some mysterious way */
And then instead of making recursive calls, we delegate some of the work to that:
/* Of course this solution also works, but is not interesting
int gcd(int a, int b)
{
return gcd2(a, b);
} */
/* So, let's do something that actually shows off the recurrence relation */
int gcd(int a, int b)
{
if (a > b)
return gcd2(a-b, b);
else if (b > a)
return gcd2(a, b-a);
else
return a;
}
(I also removed the while loop, because it is not relevant to the algorithm; of course, a return is reached in every circumstance, and this breaks the loop.)
I assume I don't need to go over the mathematical theory; and also I assume it is clear why return is needed for the gcd2 results.
But it doesn't actually matter how the work is delegated; if gcd is a function that correctly computes GCDs, and gcd2 is also such, then a call to gcd2 may be replaced by a call to gcd. This is the secret - calling a function recursively is not actually different from calling one normally. It's just that considering the possibility, requires a clearer understanding of how calling a function works and what it actually does.
Of course, it is also possible to make good use of the original while loop - by subtracting out as much as possible before doing the recursion. That might look like:
int gcd(int a, int b)
{
if (a > b)
while (a > b)
a -= b; /* prepare a value for the recursion. */
return gcd(a, b); /* and then recurse with that value. */
else if (b > a)
while (b > a)
b -= a; /* similarly. */
return gcd(a, b);
else /* a == b */
return a;
}
But then we might as well go all the way and convert to an iterative approach:
int gcd(int a, int b)
{
while (a != b)
/* whichever is greater, adjust it downward, leaving an (a, b)
pair that has the same GCD. Eventually we reach an equal pair,
for which the result is known. */
if (a > b)
a -= b;
else
b -= a;
return a; /* since the loop exited, they are equal now. */
}
(And we also could do modulo arithmetic to accomplish multiple subtractions at once; this is left as an exercise.)
The first gcd function "tries" to compute the gcd but in the end always returns a unchanged.
The second gcd function computes the gcd recursively and each invocation returns a gcd.
I simply want a program that has the user input a value for a and b, and will ask the user to repeat this process if the value a is less than b.
Here is my program:
#include <stdio.h>
#include <math.h>
int main(void)
{
int a, b ,c;
while (a<=b)
{
printf("Please enter a value for a:\n");
scanf("%d", &a);
printf("Please enter a value for b:\n");
scanf("%d", &b);
if (a<=b)
printf("a must be greater than b:\n");
}
c=a+b;
printf("The answer of c is: %d\n", c);
return 0;
}
As soon as i run the program, it prints: "The answer of c is: 1829030"
(Please note that the last number is always random)
Please help me run this program.
The value of an uninitialized non static local variable is indeterminate. This means that the value can be anything at run time. In this case, the values of a, b, c are random and the loop may or may not be entered based on the random values of a, b, c.
try -
int a = 0, b = 0, c;
You declare variables a, b, c but you do not give them an initial value. That means that they have an indeterminate value.
Right after declaring a and b with no value given, you go on and compare them in your while (a <= b) condition. C does not know how to compare these value-less variables apparently, so it skips your loop altogether.
You could avoid this problem by giving int a = 0, b = 1 values initially.
This way you make sure that the loop will at least run once.
What happens with uninitialized variables is explained in this other SO answer
As I am new to programming, I was trying to write a simple code using functions which will give me the addition of three numbers. Here's the code!
/* Your includes go here */
#include <stdio.h>
int addThreeNumbers(int a, int b, int c)
{
int d;
d = a + b + c;
return(d);
/* Complete this function only
DO NOT write main function.
*/
}
int main()
{
int x, y, z, sum;
printf("Enter the three numbers: ");
scanf(" %d %d %d", &x, &y, &z);
sum = addThreeNumbers(x, y, z);
printf("The sum is %d", sum);
return 0;
}
And the error was as follows:
solution.c:30:5: error: redefinition of ‘main’
solution.c:15:9: note: previous definition of ‘main’ was here
You have another main function in the code somewhere. Post the complete code and I will take a closer look. But that is the only way you can receive this error
In modern C, empty argument parentheses mean that the type and number of arguments is unknown.
Although this segment runs fine with most compilers, yours might be picky. Try declaring main with zero arguments explicitly, like this:
int main(void) {
//code
}
Pretty sure this is one of the online coding sites' question. They put in the main function themselves by appending it to the code, you don't have to explicitly write it. Delete the main function you have written and check if that works out.
I have following C program. I am not understanding the output of this program:
#include <stdio.h>
int find (int a, int b, int c)
{
int temp;
c=a+b;
temp=a;
a=b;
b=2*temp;
printf("%d %d %d\n", a,b,c);
return b;
}
int main()
{
int x, y, z;
x=15;
y=25;
z=30;
printf("%d %d %d %d\n", x, y, z, find(x,y,z));
return 0;
}
According to me, the output value should be:
15 25 30
25 30 40 30
But why is it:
25 30 40
15 25 30 30
So, let's walk the steps of this program:
We create and set initial values for x, y, z.
We call printf(..., find(x,y,z));
To do the work for that printf() call, we will need to fully evaluate the find() call.
find() does some work changing local values of some variables named a, b, c.
find() calls a printf() inside it, printing 25 30 40\n.
find() returns with a value of 30.
Now, we actually have the parameters for the original printf() call, we can execute it, and it prints 15 25 30 30\n.
Note that, we can't call the first printf() until we have all of the parameters to it. To do that, we need to call find() and let it fully evaluate.
First, you modify the parameters a, b, and c inside the the find function, which accounts for the modified values in the first line of output. However, those changes are only visible inside the body of find; arguments are passed by value in C. The values of x, y, and z in main are not affected by the call to find, which explains the second line of output.
Put another way, a, b, and c are local variables to the find function which are initialized by, but in no other way related to, the values of x, y, and z in main.
Before the printf in main can print its string, it must evaluate all 4 of its remaining arguments, which includes a call to find. During the course of calling find, printf is called again, which accounts for the 3-number output line appearing first.
#include<stdio.h>
float func (float t, float y){
return y ;
}
int main (){
float t0,y0,t,y;
printf ("the value of t: ");
scanf ("%f",&t0);
printf ("the value of y: ");
scanf ("%f",&y0);
t=t0;
y=y0;
static int n=0;
// t[0]=t0;
// y[0]=y0;
for (n=0;n<=3;n++){
y[1]=y[0];
printf ("value of y %f %f \n",t,y);
}
return 0;
}
The error is:
Building prog.obj.
D:\master\c language\ch3\prog.c(166): warning #2117: Old-style function definition for 'main'.
D:\master\c language\ch3\prog.c(182): error #2144: Type error: pointer expected.
D:\master\c language\ch3\prog.c(182): error #2144: Type error: pointer expected.
*** Error code: 1 ***
You cannot array index something that is not an array, or a pointer into an array.
Your y and t floats are not pointers into arrays in your program.
You should make them float *y, *t into pointers so you can point them into array.
Change float t0,y0,t,y; to float t0,y0,*t,*y;
and
t=&t0; //assign address of t0 to t
y=&y0;
Change printf ("value of y %f %f \n",t,y); to
printf ("value of y %f %f \n",*t,*y); //note to dereference t and y here, to get their values
Here's a example of your program I fixed to work
The 'Old-style function definition for main()' message means that you've not given a prototype definition. The correct forms are:
int main(void) { ... }
int main(int argc, char **argv) { ... }
The version int main() is fine in C++, but not strictly a prototype in C, and hence gets the 'old-style' tag.
The other messages are more inscrutable; the line numbers do not correspond to the code you show. However, as Tony The Lion notes in his answer, the line
y[1] = y[0];
is erroneous since y is not an array. There is room to think that should be:
y = y0;
and you'd need a companion:
t = t0;
in order to have defined values printed in the printf() statement.
Even with these changes, the code does not make a lot of sense. However, given that you removed 150-odd lines, we can suppose that the missing code would make more sense.
There is no need to make n into a static variable; it is better not to do so.
Please make sure, in future, that your error messages correspond to the source code you post, not to some variant version of the code you post. The line numbers should not be as large as 166 or 182; they should be single digit numbers or small double digit numbers. But even more importantly, they should match the code!