Point or "reset" variables in C - c

If I have some thing like:
printf("\nEnter 2 numbers: \n");
scanf(" %d %d", &a, &b);
add (a,b)
int a,b;
{
printf ("%d", a+b);
}
Then want to run the block again, but with new variables of "nothing" like when the first printf statement is entered. Any suggestions?

First of all avoid using K&R C syntax
/* Your function
add (a,b)
int a,b;
{
printf ("Sum = %d\n", a+b);
}
*/
/* Use following style*/
void add (int a,int b)
{
printf ("Sum = %d\n", a+b);
}
int main()
{
int i,a,b; // Declare variables
int n=5; // Call it say n=5 times
for(i=0;i<n;i++) //Use a for loop to iterate for n times
{
printf("\nEnter 2 numbers: \n");
if(scanf(" %d %d", &a, &b)==2) // with 2 new inputs
add(a,b); //Call your add function
}
}

Related

find the sum of 2 numbers in c with input from user

#include<stdio.h>
int main(){
int num1=0;
int num2=0;
int sum=0;
printf("enter 2 numbers\n");
scanf("%d %d",&num1,&num2);
sum=num1+num2;
printf("%d",&sum);
return 0;
}
This is what i am trying but 23+23 is coming out to be 6422292 in this way.I cant find the error. Please help.
Do NOT put an "address of" operator (&) on this line:
printf("%d",&sum);
It should be
printf("%d", sum);
Hey actually the error is in the printf() function
The & is your telling to print the value stored in the sum variable
Make the following changes to your code
printf("%d", sum);
Hope you got fixed the error
#include<stdio.h>
int main() {
int a , b;
printf ("enter a\n");
scanf ("%d", &a);
printf ("enter b\n");
scanf ("%d", &b);
int sum = a + b;
printf ("the sum is : %d", sum);
return 0;
}

C program is not adding correctly when looping over an array

I was trying to find the sum of 5 numbers (in C Language) using tutorials from "thenewboston" on Youtube. My code is:
int main(int argc, char *argv[]) {
int a, b, c, d, e;
int array[5]={a, b, c, d, e};
int sum=0;
int i;
int j;
printf("Enter your 5 numbers: ");
scanf("%d, %d, %d, %d, %d", &a, &b, &c, &d, &e);
for (i = 0; i < 5; i++){
sum+=array[i];
}
printf("The sum of 5 numbers is:%d",sum);
return 0;
}
But the weird thing was, no matter what 5 numbers I entered, I always got the sum as 48.
Either discard variables a, b, c, d, e and the array remains or vice versa
Remove variables on your first printf:
print("Enter 5 numbers: ");
Don't put variables when you did't use them.
When you put scanf as scanf("%d, %d", &var1, &var2);, you must also input the same format as
Enter 5 numbers: 10, 20
Working example(more efficient with array):
int main() {
int input[5];
int sum;
printf("Enter 5 numbers: ");
scanf("%d, %d, %d, %d, %d", &input[0], &input[1], &input[2], &input[3], &input[4]);
int i;
for (i = 0; i < 5; i++) {
sum += input[i];
}
printf("The sum is %d", sum);
return 0;
}
You aren't storing a, b, c, d, or e into the array array. You need to store them in the array after you read them in.
Your declaration of array doesn't create an array of pointers to your variables - it creates a single pointer to a contiguous block of five integer fields. You can't update those array fields by just using the addresses of a, etc., since your array doesn't point to them.
The most obvious, clear, and simple way to store them in the array (which I recommend) is:
array[0]=a;
array[1]=b;
array[2]=c;
array[3]=d;
array[4]=e;
Do this just before the beginning of your for loop.

take comparison operator as a char with scanf and use in if

#include<stdio.h>
int main()
{
int a,b;
char c;
printf("Enter value for a and b ");
scanf("%d %d ",a,b);
printf("Enter c(<) or c(>) for comparison");
scanf(" %c ",c);
if(a ? b)
{
printf("%d ",a);
}
}
I want to take char c from scanf and put it place of ? ,and I want to run if statement in this manner,in any way is it possible?
It's not possible to do something like this is C, to dynamically change the comparison operator at runtime.
Instead, you could use an approach as presented below (I also added a few fixes to the code, explained in comments):
#include<stdio.h>
int main()
{
int a, b;
char c;
printf("Enter value for a and b ");
// No spaces neeeded after specifiers, otherwise the user have to input additional data
// Pointers to the variables have to be passed to scanf
scanf("%d%d", &a, &b);
printf("Enter c(<) or c(>) for comparison ");
// A single space needed before %c , to discard previous newline
scanf(" %c", &c);
int condition = 0; // initial: false
// For every of the operators, assign the comparison result to 'condition'
if (c == '<')
condition = (a < b);
else if (c == '>')
condition = (a > b);
else
printf("Unknown operator: %c\n", c);
// Test for condition
if (condition)
{
printf("%d\n",a);
}
// Add a return statement
return 0;
}
EDIT: as Weather Vane noticed, this variant is only for c++, not c.
You can try to do something like that
#include <bits/stdc++.h>
using namespace std;
class Compare {
public:
virtual bool compare (int a, int b) = 0;
};
class Less: public Compare {
public:
bool compare (int a, int b) {
return a < b;
}
};
class More: public Compare {
public:
bool compare (int a, int b) {
return a > b;
}
};
map<char, Compare*> m;
int main() {
m['<'] = new Less();
m['>'] = new More();
int a, b;
char c;
printf("Enter value for a and b ");
scanf("%d %d ", &a, &b);
printf("Enter c(<) or c(>) for comparison");
scanf(" %c ", &c);
if ((*m[c]).compare(a, b)) {
printf("%d ", a);
} else {
printf("%d ", b);
}
}
PS sorry for using namespace std and include <bits/stdc++.h> but I didn't want to think about stuff like std:: and a lot of include's)

How to get the largest integer using modular programming?

So, I've been trying to get two integers from the user and the program should return the value of the larger one. Here is my sample program:
#include<stdio.h>
int larger(int a, int b);
int main()
{
int num1, num2;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
printf("\n%d is larger than the other", larger(num1, num2) );
}
int larger(int a, int b)
{
if (a>b)
{
printf("%d", a);
}
else if(b>a){
printf("%d", b);
}
}
But the problem here is that whenever I compile then run the program an integer value of 1 is placed beside the sentence "is larger than the other" ,while the largest integer (the integer that should be beside the 'is larger than the other') is placed above the integer 1.
SAMPLE OUTPUT:
Enter first number: 5
Enter second number: 3
5
1 is larger than the other
What should I do to place the larger value placed where it should be?
int larger(int a, int b)
Your function doesn't have a return statement . If you want largest value between two you need to return it from function . Like this -
int larger(int a, int b)
{
if (a>b)
{
//printf("%d", a); unnecessary as you print value in main
return a;
}
else if(b>a){
//printf("%d", b);
return b;
}
else
return a; //in this case take care of output message as both variables will be equal
}
larger needs to actually return the larger value: write return a;, and return b; in the appropriate places. And the output will look odd if you retain the printf calls in that function.
Currently your program behaviour is undefined as the return value is missing: the output is currently arbitrary.
You're also not dealing with all possibilities. What should happen if a and b are equal? You must return something on all control paths.
Your function isn't returning anything, you need to enable compiler warnings or alternatively, get a new compiler.
That being said, in case you aren't afraid of boolean logic, the function can simply be written as
static inline bool larger (int a, int b)
{
return a > b;
}
Naturally, you'll have to rewrite main() to check the result:
if(larger (x, y))
{
printf("x larger than y");
}
In int function larger(int a, int b) you should just compare and return the value rather than printing it.
#include<stdio.h>
int larger(int a, int b);
int main()
{
int num1, num2, largeOftheTwo= 0;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
if(num1 != num2)
printf("\n%d is larger than the other", larger(num1, num2));
else
printf("\n%d is equal to %d", num1, num2);
return 0;
}
int larger(int a, int b)
{
if (a>b)
return a;
else
return b;
}

c program to swap values printing wrong result [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
Hi I have written a c program that reads in 2 values then swaps them and prints the new values except the second value keeps showing 0. For example it you enter 10 for 'a' and 8 tor 'b', then a will be 8 but b will be 0. Does anyone know the solution to fix this? Here is the code:
#include <stdio.h>
int getData()
{
int a, b;
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
return(a, b);
}
void swapValues(int a, int b)
{
printf("The value of a is: %d", b);
printf("\nThe value of b is: %d", a);
return;
}
int main()
{
int a, b = getData();
swapValues(a, b);
return(0);
}
return (a, b);
doesn't do what you think it does, it's a misapplication of the comma operator.
The expression op1, op2 evaluates both op1 and op2 but gives you the value of op2. So it's not passing back a couple of values (although some languages like Python can do this sort of thing).
Similarly,
int a, b = getData();
won't grab the mythical two values returned from getData(). Rather it will set a to an indeterminate value and set b based on the single value returned from the function.
I would be looking at something like this:
#include <stdio.h>
int getData (char *which) {
int val;
printf ("Enter value for %s: ", which);
scanf("%d", &val);
return val;
}
void swapValues (int a, int b) {
printf("The swapped value of a is: %d\n", b);
printf("The swapped value of b is: %d\n", a);
}
int main (void) {
int a = getData ("a");
int b = getData ("b");
swapValues(a, b);
return 0;
}
You should also keep in mind that, if you actually want to swap the variables a and b and have that reflected back to main(rather than just print them as if they've been swapped), you'll need to pass pointers to them and manipulate them via the pointers.
C is a pass-by-value language meaning that changes to function parameters aren't normally reflected back to the caller. That would go something like this:
void swapValues (int *pa, int *pb) {
int tmp = *pa;
*pa = *pb;
*pb = tmp;
}
:
swapValues (&a, &b);
// a and b are now swapped.
You have unnecessarily complicated the whole thing.For one, something like return(a,b) is absurd in C.Further, if you intend to swap, why are you passing b as argument for the printf() meant to print 'a' and passing a to the printf() of 'b'?Anyways,here's a modified code that keeps it simple and gets the job done.
#include <stdio.h>
void swapValues()
{
int a, b,tem;
printf("Enter first number: ");
scanf("%d", &a);
printf("\nEnter second number: ");
scanf("%d", &b);
tem=a;
a=b;
b=tem;
printf("\nThe value of a is: %d", a);
printf("\nThe value of b is: %d", b);
}
int main()
{
swapValues();
return(0);
}
First of all you can't return more than one value in C. The way around that is to return a struct or pass the values address.
void getData(int *a,int* b)
{
//int a, b;
printf("Enter first number: ");
scanf("%d", a); // look here you passed the address of a to scanf
// by doing that scanf can write to a
printf("Enter second number: ");
scanf("%d", b);
//return(a, b);
}
The old main:
int main()
{
int a, b = getData(); // b gets the return value from getData()
// but a is still uninitialized
//to call the new function you have to do the following
int a,b;
getData(&a,&b);
swapValues(a, b);
return(0);
}
You cannot return multiple values from a C function. I'm not even sure why the statement return(a, b) compiles.
If you want to return more than value from a function you should either put them into an array or a structure. I'm going to use a structure to demonstrate one way to do this correctly. There are many ways to do this, but this one modifies you code the least.
struct TwoNums{
int a;
int b;
};
TwoNums getData()
{
/* This creates a new struct of type struct TwoNums */
struct TwoNums nums;
printf("Enter first number: ");
scanf("%d", &(nums.a));
printf("Enter second number: ");
scanf("%d", &(nums.b));
return(a, b);
}
void swapValues(int a, int b)
{
printf("The value of a is: %d", b);
printf("\nThe value of b is: %d", a);
return;
}
int main()
{
/* Get the whole structure in one call */
struct TwoNums nums = getData();
/* Call the swap function using fields of the structure */
swapValues(nums.a, nums.b);
return 0;
}
The first:
getData() function is written incorrectly.
You can not return more than one parameter from the function in C. So you can to separate data reading, or use pointers as below:
void getData(int* a, int* b) {
printf("Enter first number: ");
scanf("%d", a);
printf("Enter second number: ");
scanf("%d", b);
}
In main()
int a, b;
getData(&a, &b);
The second:
swapValues(int a, int b) does not swap the data.
More correct:
void swapValues(int* a, int* b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
without using temporary variable.
So try this code
#include <stdio.h>
int swape()
{
int a,b;
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
a=a+b;
b=a-b;
a=a-b;
printf("The value of a is: %d", a);
printf("\nThe value of b is: %d", b);
}
int main()
{
swape();
return(0);
}

Resources