So, I'm trying to put the output of one function and put it to another function
Here are the functions that I'm trying to get output and inputs, you can just ignore inside of a second function I just simply put printf to check if the variables are correct.
int guess(int a, int b){
printf("\nEnter you guess: ");
scanf("%d,%d", &a, &b);
return a, b;
}
int check(int a, int b){
printf("%d %d ",a,b);
}
And here is the code:
#include <stdio.h>
int main(){
int row, column;
guess(row, column);
check(row, column);
}
int guess(int a, int b){
printf("\nEnter you guess: ");
scanf("%d,%d", &a, &b);
return a, b;
}
int check(int a, int b){
printf("%d %d ",a,b);
}
I tried to put it simply to understand how to do it more clearly.
When I run the code and put coordinates for example: 4,5 and it only prints out 0 1
Also, Is it possible to do it with arrays?
1) You need to use a prototype or declare your functions forward.
2) You can not return 2 variables form a function in C, but you can pass an array and read/write his values:
#include <stdio.h>
void guess(int arr[])
{
printf("\nEnter you guess: ");
scanf("%d,%d", &arr[0], &arr[1]);
}
void check(int arr[])
{
printf("%d %d ",arr[0], arr[1]);
}
int main(void)
{
int arr[2];
guess(arr);
check(arr);
return 0;
}
or you can pass a reference
void guess(int *a, int *b)
{
printf("\nEnter you guess: ");
scanf("%d,%d", a, b);
}
guess(&a, &b);
The input numbers you read in guess functions are actually only read into the local variables a and b. You'd need to pass pointers to be able to read into the vars in main.
Also there's no way to return multiple values from a function in C.
#include <stdio.h>
void guess(int *a, int *b)
{
printf("\nEnter you guess: ");
scanf("%d,%d", a, b);
}
int check(int a, int b)
{
printf("%d %d " ,a, b);
}
int main()
{
int row = 0, column = 0;
guess(&row, &column);
check(row, column);
}
You should also check the return value of scanf for failures.
Related
#include <stdio.h>
#include <conio.h>
int sum();
//find the sum of two numbers entered by the user.
int main(){
int n, m;
printf("Enter two numbers: \n");
scanf("%d", &m);
scanf("%d", &n);
int result = sum(m, n);
printf(result);
getch();
return 0;
}
int sum(m, n){
int c;
c = m+n;
return c;
}
i was just writing a simple program with function but i don't know why it is not running it tells me to debug can someone tell me what is the problem with it
Change int sum() ; to int sum(int, int) ;
Change printf(result) to printf("%d", result) ;
Change int sum(m, n) to int sum(int m, int n) ;(https://i.stack.imgur.com/Z07cx.jpg)
Another way of writing above program is
(https://i.stack.imgur.com/VOXRY.jpg)
#include <stdio.h>
#include <conio.h>
int sum(int n, int m);
//find the sum of two numbers entered by the user.
int main(){
int n, m;
printf("Enter two numbers: \n");
scanf("%d", &m);
scanf("%d", &n);
int result = sum(m, n);
printf("%d",result);
getch();
return 0;
}
int sum(int n, int m){
int c;
c = m+n;
return c;
}
I was wondering how I can input the numbers using a function with the code written below, and a bit stuck on how I can input and give it an output I am just starting out on functions level 0 at it basically.
int addTwoInt(int a, int b);
int main(void)
{
printf("Enter a number: ");
scanf("%d", &addTwoInt(<#int a#>, <#int b#>));
// printf("The two numbers added are %d", addTwoInt);
}
int addTwoInt(int a, int b)
{
int sum;
sum = a + b;
return sum;
printf("The sum of the numbers are %d", sum);
}
int addTwoInt(int a, int b);
int main(void)
{
int x;
int y;
printf("Enter a number: ");
scanf("%d", &x);
scanf("%d", &y);
int z = addTwoInt(x, y);
printf("%d", z);
//printf("The two numbers added are %d", addTwoInt);
}
int addTwoInt(int a, int b)
{
int sum;
sum = a + b;
printf("The sum of the numbers are %d", sum);
return sum;
}
You asked for cleaner way to add two numbers or other arithmetic operations u can simply do it in return statement just like this:
int addTwoInts(int a, int b){
return a+b
}
Only Value of 1st row of matrix is passing through Add().I am getting erroneous results.for Eg : matrix 1 has elements 1,2,3,4 .However when i print these values in add() ,I am getting 1,2,0,0.
#include <stdio.h>
int main()
{
int arr[100][100], arr1[100][100];
int m, n, x, y, z;
printf("\n Enter order of matrix");
scanf("%d %d ", &m, &n);
//ENTRY OF MATRIX
printf("\n Enter Values of Matrix 1");
for (x = 0; x < m; x++)
{
for (y = 0; y < n; y++)
scanf("%d ", &arr[x][y]);
}
printf("\n Enter Value of Matrix two ");
for (x = 0; x < m; x++)
{
for (y = 0; y < n; y++)
scanf("%d ", &arr1[x][y]);
}
add(m, n, arr, arr1);
return 0;
}
void add(int m, int n, int a[m][n], int b[m][n])
{
int x, y;
for (x = 0; x < m; x++)
{
for (y = 0; y < n; y++)
printf("%d ", a[x][y]);
printf("\n");
}
}
In C, functions must be declared before use and the type of the arguments must match between declaration and call.
In OP's code, a couple of 2D array declared as
int arr[100][100];
are passed to a function defined after main() as
void add(int m, int n, a[m][n], b[m][n]) { /* ... */ }
Generating two issues:
add() is used in main(), but it's not declared before, it's defined only after.
arr and arr1 are 2D arrays of 100 x 100 ints, but function add() as written, accepts two Variable Length Arrays of size m x n.
OP have two ways to fix, either they change the interface of the function and declare it before main(), like this:
void add(int m, int n, a[][100], b[][100]); // and define after main
Or change the declarations of arr and arr1:
// includes...
void add(int m, int n, a[m][n], b[m][n]);
int main(void)
{
int m, n;
// ...
// read n and m before using them to declare the two VLA
int arr[m][n], arr1[m][n];
// ...
add(m, n, arr, arr1);
// ...
}
void add(int m, int n, a[m][n], b[m][n])
{
// ...
}
First of all, please fix your indentation, it makes your code really hard to read.
This error is because you can't use a function that has not been declared.
To fix that, either add a prototype after your includes :
void add(int m,int n,int a[m][n],int b[m][n]);
Or try to put your whole function before your main.
I have fixed some problem in your code.I have added declaration before main().
void add(int m,int n,int a[m][n],int b[m][n]);
Also, I have removed the whitespace into scanf function.
int main()
{
int arr[100][100],arr1[100][100];
int m,n,x,y,z;
printf("\n Enter order of matrix");
scanf("%d %d",&m,&n);
//ENTRY OF MATRIX
printf("\n Enter Values of Matrix 1");
for(x=0;x<m;x++)
{
for(y=0;y<n;y++)
scanf("%d",&arr[x][y]);
}
printf("\n Enter Value of Matrix two ");
for(x=0;x<m;x++)
{
for(y=0;y<n;y++)
scanf("%d",&arr1[x][y]);
}
add(m,n,arr,arr1);
return 0;
}
void add(int m,int n,int a[m][n],int b[m][n])
{
// Your operation
}
I am trying to pause my screen to test code but i don't know were to put system("pause") anywhere I put it says undefined
#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS
void load(int*a, int*b, int*c)
{
printf("Enter 3 numbers");
scanf("%d %d %d", &(*a), &(*b), &*(c));
}
void calc(int a, int b, int c, int *sum, float *avg)
{
*sum = a + b + c;
*avg = *sum / (float)3;
}
void print(int a, int b, int c, int sum, float avg)
{
printf("The 3 numbers are%d %d %d \n",a, b, c);
printf("The sum is %d\n", sum);
printf("The Avg is %f\n", avg);
}
void main()
{
int a, b, c, sum;
float avg;
load(&a, &b, &c);
calc(a, b, c, &sum, &avg);
print(a, b, c, sum,avg);
}
Put it in the end of main().
And donnot forget to include its header:
#include <stdlib.h>
While trying to do the GCD and LCM program from programming simplified...I am facing problems with the results. I did everything correct(according to me) and even checked word by word but the problem still persists...I am pasting the code of normal method only.
#include <stdio.h>
int main()
{
int a, b, x, y, t, gcd, lcm;
printf("Enter first number :");
scanf("%d", &a);
printf("Enter first number :");
scanf("%d", &b);
a = x;
b = y;
while (b != 0)
{
t = b;
b = a % b;
a = t;
}
gcd = a;
lcm = (x * y)/gcd;
printf("Greatest common divisior of %d and %d = %d\n", x, y, gcd);
printf("Lowest common divisior of %d and %d = %d\n", x, y, lcm);
getch();
}
At least this part is fundamentally wrong:
int a, b, x, y, t, gcd, lcm;
printf("Enter first number :");
scanf("%d", &a);
printf("Enter first number :");
scanf("%d", &b);
a = x;
b = y;
So you're declaring x and y uninitialized, then you're assigning them to a and b - now a and b don't contain the values the user entered, but some garbage. You probably want
x = a;
y = b;
instead.
Better try this. This is simpler to run.
#include<stdio.h>
int GCD(int,int);
void main(){
int p,q;
printf("Enter the two numbers: ");
scanf("%d %d",&p,&q);
printf("\nThe GCD is %d",GCD(p,q));
printf("\nThe LCM is %d",(p*q)/(GCD(p,q)));
}
int GCD(int x,int y){
if(x<y)
GCD(y,x);
if(x%y==0)
return y;
else{
GCD(y,x%y);
}
}
Try it
#include<stdio.h>
int main(){
int a,b,lcm,gcd;
printf("enter two value:\n");
scanf("%d%d",&a,&b);
gcd=GCD(a,b);
lcm=LCM(a,b);
printf("LCM=%d and GCD=%d",lcm,gcd);
return 0;
}
int GCD(int a, int b){
while(a!=b){
if(a>b){
a=a-b;
}else{
b=b-a;
}
}
return a;
}
int LCM(int a, int b){
return (a*b)/GCD(a,b);
}