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>
Related
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.
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
}
I am given 5 integers that must be not less than 10 and not more than 100. Then, I must find their division remainders if we divide them by 10. Then, I must find the sum of the remainders that I found and (there it goes the difficult part) finally which one of the 5 remainders is the max.
Here, I give you the code that I wrote but I have no idea how to go further. Maybe with "for", but I don't imagine how exactly.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a, b, c, d, e, a10,b10,c10,d10,e10, sum, max;
printf("give 5 integers \n");
scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
a10 = a % 10;
b10 = b % 10;
c10 = c % 10;
d10 = d % 10;
e10 = e % 10;
printf("division remainder is: %d %d %d %d %d\n",
a10,b10,c10,d10,e10);
sum = a10 + b10 + c10 + d10 + e10;
printf("the sum of the remains is: %d\n", sum);
system("pause");
}
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
int *rands_range(int n, int low, int high);
void print(FILE *fp, int n, int *nums);
int *map(int n, int *nums, int (*f)(int));
int fold(int n, int *nums, int init, int (*f)(int, int));
int add(int x, int y);
int rem10(int x);
int greater(int x, int y);
int main(void){
srand(time(NULL));
int n = 5;
printf("give 5 integers\n");
int *integers = rands_range(n, 10, 100);
print(stdout, n, integers);
putchar('\n');
printf("division remainder is: ");
int *rems = map(n, integers, rem10);
print(stdout, n, rems);
putchar('\n');
int sum = fold(n, rems, 0, add);
printf("the sum of the remains is: %d\n", sum);
putchar('\n');
int max = fold(n, rems, *rems, greater);
printf("the max of the remains is: %d\n", max);
putchar('\n');
free(integers);
free(rems);
system("pause");
}
int *rands_range(int n, int low, int high){
assert(n > 0 && RAND_MAX >= high - low && high >= low);
int *values = malloc(n * sizeof(*values));
if(values){
for(int i = 0; i < n; ++i){
values[i] = low + rand() % (high-low+1);
}
}
return values;
}
void print(FILE *fp, int n, int *nums){
for(int i = 0; i < n; ++i){
if(i)
fprintf(fp, ", ");
fprintf(fp, "%d", nums[i]);
}
fprintf(fp, "\n");
}
int remainder_int(int x, int y){
return x % y;
}
int add(int x, int y){
return x + y;
}
int rem10(int x){
return remainder_int(x, 10);
}
int greater(int x, int y){
return x > y ? x : y;
}
int *map(int n, int *nums, int (*f)(int)){
int *result = malloc(n * sizeof(*result));
if(result){
for(int i = 0; i < n; ++i){
result[i] = f(nums[i]);
}
}
return result;
}
int fold(int n, int *nums, int init, int (*f)(int, int)){
int acc = init;
for(int i = 0; i < n; ++i){
acc = f(acc, nums[i]);
}
return acc;
}
Functions with variable arguments:
#include <stdio.h>
#include <stdarg.h>
#include <limits.h>
int max(int n, ...){
int ret = INT_MIN;
va_list ap;
va_start(ap, n);
while(n--){
int wk = va_arg(ap, int);
if(ret < wk)
ret = wk;
}
va_end(ap);
return ret;
}
int sum(int n, ...){
int sum = 0;
va_list ap;
va_start(ap, n);
while(n--){
sum += va_arg(ap, int);
}
va_end(ap);
return sum;
}
int main(void){
int a = 6, b = 3, c = 9, d = 2, e = 8;
printf("sum:%d\n", sum(5, a, b, c, d, e));
printf("max:%d\n", max(5, a, b, c, d, e));
}
Following is the code using 'for' and int array. To use 'for' you must need to use array.
#include<stdio.h>
#include <stdlib.h>
int main()
{
int i;
int max;
int sum=0;
int aryNum[5];
int aryRem[5];
printf("give 5 integers \n");
for(i=0;i<5;i++)
{
scanf("%d",&aryNum[i]);
}
printf("division remainder is: ");
for(i=0;i<5;i++)
{
aryRem[i]=aryNum[i]%10;
printf("%d ",aryRem[i]);
}
printf("\n");
max=aryRem[0];
sum = max;
for(i=1;i<5;i++)
{
if(max<aryRem[i])
{
max=aryRem[i];
}
sum+=aryRem[i];
}
printf("the sum of the remains is: %d\n", sum);
printf("maximum remains is: %d\n", max);
system("pause");
return 0;
}
If you don't want to use 'for' and int array then you can use following code. But it is not advisable:
#include <stdio.h>
#include <stdlib.h>
int greater(int a, int b)
{
return (a>b)? a:b;
}
int main()
{
int a, b, c, d, e, a10,b10,c10,d10,e10, sum, max;
printf("give 5 integers \n");
scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
a10 = a % 10;
b10 = b % 10;
c10 = c % 10;
d10 = d % 10;
e10 = e % 10;
printf("division remainder is: %d %d %d %d %d\n",
a10,b10,c10,d10,e10);
sum = a10 + b10 + c10 + d10 + e10;
printf("the sum of the remains is: %d\n", sum);
/*New added code*/
max=greater(a10,b10);
max=greater(max,c10);
max=greater(max,d10);
max=greater(max,e10);
printf("maximum remains is: %d\n", max);
system("pause");
return 0;
}
I was coding C about Pollard's rho algorithm from wiki, Pollard's rho algorithms for logarithms. But I had an runtime error which I don't know. I think it might be from pointer. Would you find the error?
#include<stdio.h>
#include<math.h>
int alpha, beta, N;
void xab(int *x, int *a, int *b)
{
switch(*x%3){
case 0: *x=((*x)*(*x))%N; *a=((*a)*2)%N; *b=((*b)*2)%N; break;
case 1: *x=(alpha*(*x))%N; *a=((*a)+1)%N; break;
case 2: *x=(beta*(*x))%N; *b=((*b)+1)%N; break;
}
}
int main(void)
{
int x=1; int a=0; int b=0;
int X=1; int A=0; int B=0;
int i;
scanf("%d %d %d", alpha, beta, N);
for(i=1;i<N;i++){
xab(&x,&a,&b);
xab(&X,&A,&B); xab(&X,&A,&B);
if(x=X) break;
}
return 0;
}
scanf("%d %d %d", alpha, beta, N);
should be
scanf("%d %d %d", &alpha, &beta, &N);
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);
}