Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
#include<stdio.h>
int subtractNumbers(int n1,int n2);
int main(){
int n1,n2,difference;
printf("Enter Two Numbers: ");
scanf("%i%i",&n1,&n2);
subtractNumbers(n1,n2);
printf("The Difference is %i", n1,n2);
}
int subtractNumbers(int n1,int n2)
{
int result;
result =n1-n2;
return result;
}
Why can't this code properly do a subtraction?
Have to store the return value in 'difference' variable. (As I guess the 'difference' variable is meant for that)
In case of 'printf("The Difference is %i", n1,n2);' I guess the intention is to print the 'difference'. So change to 'printf("The Difference is %i", difference);'
#include<stdio.h>
int subtractNumbers(int n1,int n2);
int main(){
int n1,n2,difference;
printf("Enter Two Numbers: ");
scanf("%i%i",&n1,&n2);
difference = subtractNumbers(n1,n2); // stored the returend result
printf("The Difference is %i", difference); //printing only the result
}
int subtractNumbers(int n1,int n2)
{
int result;
result =n1-n2;
return result;
}
So basically you've mixed up the syntax. It's all right, but when you call your function, it's ok that you return a value, but the in your main you don't read that value.
This fixes it:
#include<stdio.h>
int subtractNumbers(int n1,int n2);
int main(){
int n1,n2,difference;
printf("Enter the first number : ");
scanf("%d",&n1);
printf("Enter the second number: ");
scanf("%d", &n2);
int result = subtractNumbers(n1,n2);
printf("The Difference is %d", result);
}
int subtractNumbers(int n1,int n2)
{
int result;
result = n1-n2;
return result;
}
Primary problem corrected like other answers, but with more commentary and a few "upgrades"...
#include <stdio.h>
// Please spread things out for your readers. Whitespace is free!
// if function PRECEEDS its use, it is its own "function prototype"
int subtractNumbers( int n1, int n2 )
{
int result = n1 - n2; // declare and assign together
return result;
}
int main() {
printf( "Enter Two Numbers: " );
// define variables "close" to when they are first used
int n1, n2;
// ALWAYS check return codes from system functions. Things go wrong
if( scanf( "%i%i", &n1, &n2 ) != 2 )
printf( "Something went wrong\n" );
else
{
// here was where the returned value was not being received. fixed.
int difference = subtractNumbers( n1, n2 );
printf( "The Difference is %i", difference );
}
return 0; // optional with some compilers
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm trying to write a program that calculates 1+2+3...+n with recursion. I'm just starting to learn C so I'm pretty bad...
Here's what I have right now:
#include <stdio.h>
int adder;
adder=1;
int sum(int numba) {
if (adder==numba) {
return(sum);
}
return(sum+adder);
++adder;
}
int main() {
char line[100];
int nummba;
printf("Enter in a number: ");
fgets(line, sizeof(line), stdin);
sscanf(line, "%d", nummba);
printf("The sum of numbers from 1 to %d is %d.", nummba, sum(nummba));
return(0);
}
Obviously doesn't work...
How do I use recursion to solve 1+2+3...n?
Thanks.
For starters there is no need to use global variables.
Secondly in these statements
return(sum);
and
return(sum+adder);
sum is a pointer to function. So the expressions of the return statements do not make sense.
Moreover this code snippet
int adder;
adder=1;
will not even compile because you may not use statements in the file scope. At least you should write
int adder = 1;
The function can look the following way
unsigned long long int sum( unsigned int n )
{
return n == 0 ? 0 : n + sum( n - 1 );
}
Here is a demonstrative program.
#include <stdio.h>
unsigned long long int sum( unsigned int n )
{
return n == 0 ? 0 : n + sum( n - 1 );
}
int main(void)
{
printf( "Enter in a non-negative number: " );
unsigned int n = 0;
scanf( "%u", &n );
printf( "The sum of numbers from 0 to %u is %llu.\n", n, sum( n ) );
return 0;
}
Its output might look like
Enter in a non-negative number: 10
The sum of numbers from 0 to 10 is 55.
Recursion requires that a function calls itself. You are not doing this, so you do not have a recursive function in the first place. Consider the following code:
#include <stdio.h>
int sum(int n) {
if (n == 0)
return n;
else
return n + sum(n - 1);
}
int main() {
int n;
char line[100];
printf("Enter in a number: ");
fgets(line, sizeof(line), stdin);
sscanf(line, "%d", &n);
printf("The sum of numbers from 1 to %d is %d.\n", n, sum(n));
return(0);
}
If you find recursion a bit difficult for now, you can opt for a less difficult solution using a while loop:
int sum(int n)
{
int i = 0;
int j = 1;
while (j <= n)
{
i += j++;
}
return i;
}
change your code to:
#include <stdio.h>
int sum_val=0;
void sum(int numba)
{
if(numba>=1)
{
sum_val=sum_val+numba;
sum(numba-1);
}
}
int main()
{
int nummba;
printf("Enter in a number: ");
scanf(" %d",&nummba);
sum(nummba);
printf("The sum of numbers from 1 to %d is %d.", nummba, sum_val);
return(0);
}
Did you compile your code?
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
this code is showing some syntactical errors I can't find out where it is, may be it is in the gcd function. here is my code.
#include<stdio.h>
#include<conio.h>
int main(void)
int gcd(int,int);
{
int a,b, temp;
printf("Enter the value of a");
scanf("%d",&a);
printf("Enter the value of b");
scanf("%d",&b);
if (b>=a)
{
int temp;
temp=b;
b=a;
a=temp;
}
elseif(b!=0)
{
gcd(a,b);
printf("The Gcd of the numbere is %d",gcd(a,b));
}
else
{
printf("The GCD of %d %d is %d:",a,b,a);
}
getch();
return 0;
}
int gcd(int a,int b)
{
int g;
while(b!=0)
{
g=a%b;
a=b;
b=g;
return g;
}
}
I would be thankful if you point out my errors and explain with the correct code.
switch the position of these two lines:
int main(void)
int gcd(int,int);
also, elseif -> else if
The gcd function uses Euclid's Algorithm. It computes a mod b, then swaps a and b with an XOR swap.
Reference
#include<stdio.h>
int gcd(int ,int );
int main(void)
{
int a,b, temp;
printf("Enter the value of a : ");
scanf("%d",&a);
printf("Enter the value of b : ");
scanf("%d",&b);
int res = gcd(a,b);
printf("The Gcd of the numbere is : %d \n",res);
return 0;
}
int gcd(int a, int b)
{
int temp;
while (b != 0)
{
temp = a % b;
a = b;
b = temp;
}
return a;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Just in case you need to know what the program I'm working on -It's a homework question:A five-digit number is entered through the keyboard. Write a function to obtain the reversed number and another function to determine whether the original and reversed numbers are equal or not. Use this functions inside the main() and provide the necessary arguments to get a result.
My code is:
#include <stdio.h>
int Reversed(int rev);
int Equality(int equ);
int main (){
int num,result;
printf("Please enter a number that has five digits:");
scanf("%d", &num);
result=Equality(num);
return 0;
}
int Reversed(int num){
int number=num;
int rev=0;
int digit;
do{
digit=num%10;
rev=rev*10+digit;
num=num/10;
}
while ((num>0));
return rev;
}
int Equality(num){
int reve,numb;
if ( (numb=num)== (reve=Reversed(num);) )
printf("number equals the reversed number");
else
printf("number doesn't equal the reversed number");
}
There are some points to fix:
Remove semicolon in if ( (numb=num)== (reve=Reversed(num);) ), which is not ought to be. ( the main problem)
Format your code properly.
Equality() doesn't have return statement, so what it returns is not usable.
I guess Equality() is supposed to only determine that, not to print the result.
The variables reve and numb in Equality(), and number in Reversed() are not used other than being assigned, so you won't need them.
Type of function arguments shouldn't be omitted.
corrected code:
#include <stdio.h>
int Reversed(int rev);
int Equality(int equ);
int main (void){
int num;
printf("Please enter a number that has five digits:");
if(scanf("%d", &num) != 1){
puts("read error");
return 1;
}
if(Equality(num))
printf("number equals the reversed number");
else
printf("number doesn't equal the reversed number");
return 0;
}
int Reversed(int num){
int rev=0;
int digit;
do{
digit=num%10;
rev=rev*10+digit;
num=num/10;
}
while (num>0);
return rev;
}
int Equality(int num){
return ( num == Reversed(num) );
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm trying to run a simple code, that gets two numbers from the user and prints their sum
there it is:
#include <stdio.h>
#include <conio.h>
void main() {
int n1 = 0, n2 = 0, sum = 0;
printf("Insert two numbers:\n");
scanf("%d %d", n1, n2);
sum = n1 + n2;
printf("The sum of %d and %d is - %d\n", n1, n2, sum);
printf("%d + %d = %d", n1, n2, sum);
getch();
}
I'm trying to run it, when i do the first print command gets executed and when i press enter for it to scan it crashes, I'm using code::blocks 13.12 to compile and run
thank you! :)
Your program crashes because you are scanning numbers to wrong memory addresses. Change
scanf("%d %d", n1, n2);
to
scanf("%d %d", &n1, &n2);
Your program doesn't work because scanf takes a pointer to int, for each "%d" specifier, so pass the address of n1 and n2 instead of n1 and n2.
#include <stdio.h>
#include <conio.h>
void main() {
int n1 = 0, n2 = 0, sum = 0;
printf("Insert two numbers:\n");
scanf("%d %d", &n1, &n2);
/* ^ ^ address of operater, take the addreess of n2 and pass it */
/* | address of operater, take the addreess of n1 and pass it */
sum = n1 + n2;
printf("The sum of %d and %d is - %d\n", n1, n2, sum);
printf("%d + %d = %d", n1, n2, sum);
getch();
}
but not just that, you should check that in fact the to numbers where read, otherwise your program will invoke undefined behavior, to make sure you read the integers successfuly you should check the return value of scanf it returns the number of arguments matched, so
#include <stdio.h>
#include <conio.h>
void main() {
int n1 = 0, n2 = 0, sum = 0;
printf("Insert two numbers:\n");
/* if it equals two it succeeded, since we requested two integers */
if (scanf("%d %d", &n1, &n2) == 2)
{
sum = n1 + n2;
printf("The sum of %d and %d is - %d\n", n1, n2, sum);
printf("%d + %d = %d", n1, n2, sum);
}
else
{
printf("error reading 2 integers\n");
return 1; /* non zero means error */
}
getch();
/* return success from main() */
return 0;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
#include <stdio.h>
#include <stdlib.h>
#define N 10
float func ( int *arr, int n, int *count )
{
int *p,i;
float sum=0;
p=arr;
for (i=0;i<n;i++)
{
sum += *p;
p++;
}
sum /= n;
count=0;
p=arr;
for(i=0;i<n;i++) //לולאה שעוברת על כל המערך ומוסיפה כל תוכן של איברבמערך שיותר גדול מהממוצע
{
*count+= (*p>sum);
p++;
}
return sum;
}
void main()
{
int i, count=0, arr[N]={0}, n=N;
for(i=0;i<N;i++)
{
printf("Please enter your grade\n");
scanf("%d",&arr[i]);
}
for(i=0;i<N;i++)
printf("%d ",arr[i]);
printf("The average in class is: %f and the number of students that had the best grades(MORE) are: %d \n",func(arr,n,count),count);
}
I need some help on this program.
the code is above,
the program need to get from the users 10 grades and then print average and count how many grades is more then the average
Your main function is incorrectly declared. You should compile with all warnings and debug info (e.g. gcc -Wall -g if using GCC, which is probably used by your Codeblocks IDE...) then you should use the debugger (e.g. gdb). And you should test the result of scanf(3)
BTW,
count = 0; // better written as count = NULL;
is wrong: it is clearing the pointer. You probably want
*count = 0;
Also, your last printf is supposing some order of evaluation (since you expect the call to func to change count before printing count), so is undefined behavior. You need:
float avg = func(arr,n,&count);
// from http://stackoverflow.com/a/25382154/841108
printf("The average in class is: %f and the number"
" of students that had the best grades are: %d \n",
avg,count);
Plese show or tell your teacher that you got help on SO (he will find out anyway)
BTW, I can't understand why students are asking their homework on the web. They don't learn anything by doing that, and their teacher will notice anyway.
You are calculating sum and count in func, so you can't return two values at a time. so count the marks which are greater then average in main().
Try this-
#include <stdio.h>
#include <stdlib.h>
#define N 10
float func ( int *arr, int n)
{
int *p,i;
float sum=0;
p=arr;
for (i=0;i<n;i++)
{
sum += *p;
p++;
}
sum /= n;
printf("%lf \n",sum);
return sum;
}
int countfun(int *arr, float sum)
{
int i,count = 0;
for(i=0;i<N;i++)
{
if(arr[i] > sum)
count++;
}
return count;
}
void main()
{
int i, count=0, arr[N]={0}, n=N;
float sum=0;
for(i=0;i<N;i++)
{
printf("Please enter your grade\n");
scanf("%d",&arr[i]);
}
for(i=0;i<N;i++)
printf("%d ",arr[i]);
sum = func(arr,n);
count = countfun(arr,sum);
printf("The average in class is: %f and the number of students that had the best grades(MORE) are: %d \n",sum,count);
}
You can use a getchar() just after the last printf() to wait for a character.
You think that the execution window closes without printing. That is wrong. It does print and then exits before you can even see the output.
P:S - You have some problems with your code as mentioned in the rest of the answers. Fix them and then try this