Scan multiple variables depending on test cases? - c

I want to calculate sum of arithmetic progression in which we have to take 3 variables from user. a=first number, b= step size/increment, c=length of sequence.
If there are more than 1 test case , say three, then I have to scan a,b,c three time. How to do this?
E.g scanf (" %d %d %d", a,b,c); 3 times without affect initial values in first test case.

If you know no of test cases read it first and store it in a variable.
int calculate_ap(int a, int b, int c)
{
//Implement function to calculate Arithmetic progression and return the result
}
int main()
{
int test_cases = 0;
int a, b, c;
scanf("%d", &test_cases); //Reads no of test cases
while(test_cases--)
{
scanf("%d, %d, %d", &a, &b, &c); //read A, B, C
printf("%d\n", calculate_ap(a, b, c));
}
}
Hope this helps.

Related

Using Scanf with pointers

I am fairly new to C and I am having trouble using scanf with pointers. I have been told to get user inputs for 3 int and 1 char values and then print them back out using pointers.
This is the best I could come up with so far:
int a, b, c;
char d;
int *x = &a;
int *y = &b;
int *z = &c;
char *e = &d;
scanf("Enter 3 Ints and 1 Char:%d %d %d %c", x, y, z, e);
printf("The numbers are:\n");
printf("%d\n %d\n %d\n %c\n", a, b, c, d);
return 0;
When I enter the values the following is printed out:
2 3 4 c
The numbers are:
32708
-613084440
32708
�
Again, I'm very new to programming so I apologize if this is a stupid mistake or something obvious that I have missed.
You are not checking the return value of your scanf, otherwise you would know that it returns 0, as in 'no elements read'.
Your scanf expects you to write exactly what you are putting in there, so, if you entered Enter 3 Ints and 1 Char:2 3 4 c, it would probably work.
What you want, however, is this:
printf("Enter 3 Ints and 1 Char: ");
if (scanf("%d %d %d %c", &a, &b, &c, &d) != 4)
printf("Invalid input detected\n");
else
printf("The numbers are:\n%d\n %d\n %d\n %c\n", a, b, c, d);
The first line will print the prompt to the console, the second will read the values into variables.
There is no need to create separate pointer variables for this purpose.

Enter the value of two number

Actually, I'm learning C langage and I have written a programm
Enter the value of 2 numbers as shown below.
#include<stdio.h>
int main()
{
int a, b, c;
printf("Enter two numbers to add\n");
scanf("%d%d", &a, &b);
printf("Sum of the numbers = %d\n", c);
return 0;
}
But if I enter an alphabet I'm getting some 1522222 numbers. Instead of
this I want it throws an error as invalid input if I type alphabet ie a,b,c.
How could I do that?
You can check the return value of scanf. If it is successful, it should return 2, since you're reading two values. If you get anything else, you know that the input is incorrect. Try this:
if (scanf("%d%d", &a, &b) != 2)
printf("Invalid input type!\n");
else
printf("Sum of the numbers = %d\n", a+b);
On a different note, you don't initialize c anywhere, so printing it is undefined behavior. You don't even need c for this, you can just print a+b instead.

Returns a value as an output parameter

I have a question in C where I need to insert coefficients of a quadratic equation into a function and return the number of solutions and result.
Write a program that accepts a series of 3 real numbers, which are the
coefficients of a quadratic equation, and the program will print out
some solutions to the equation and the solutions themselves.
Guidelines:
Functions must be worked with one of the functions that
returns the number of solutions as a returned value, and returns the
solutions themselves through output parameters.
3 numbers must be
received each time. The input will be from a file (will end in EOF)
In the meantime I built the function without reading from a file just to see that it works for me, I built the function that returns the number of solutions but I got entangled in how to return the result as output parameter
here is my code for now:
int main ()
{
double a, b, c, root1,root2,rootnum;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf",&a, &b, &c);
rootnum=(rootnumber(a,b,c);
printf("the number of roots for this equation is %d ",rootnum);
}
int rootnumber (double a,double b, double c)
{
formula=b*b - 4*a*c;
if (formula<0)
return 0;
if (formula==0)
return 1;
else
return 2;
}
In C, providing an "output parameter" usually amounts to providing an argument that is a pointer. The function dereferences that pointer and writes the result. For example;
int some_func(double x, double *y)
{
*y = 2*x;
return 1;
}
The caller must generally provide an address (e.g. of a variable) that will receive the result. For example;
int main()
{
double result;
if (some_func(2.0, &result) == 1)
printf("%lf\n", result);
else
printf("Uh oh!\n");
return 0;
}
I've deliberately provided an example that illustrates what an "output parameter" is, but has not relationship to the code you actually need to write. For your problem, you will need to provide two (i.e. a total of five arguments, three that you are providing already, and another two pointers that are used to return values to the caller).
Since this is a homework exercise, I won't explain WHAT values your function needs to return via output parameters. After all, that is part of the exercise, and the purpose is for you to learn by working that out.
Apart from a wayward parenthesis in the call and some other syntax errors, what you have so far looks fine. To print out the number of roots, you need to put a format specifier and an argument in your printf statement:
printf("the number of roots for this equation is %d\n", rootNum);
The %d is the format specifier for an int.
Here is your working code:
#include <stdio.h>
int rootnumber (double a,double b, double c)
{
double formula = (b*b) - (4*(a)*(c));
if (formula > 0) {
return 2;
}
else if (formula < 0) {
return 0;
}
else {
return 1;
}
}
int main (void)
{
double a, b, c;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf",&a, &b, &c);
printf("The number of roots for this equation is %d ", rootnumber(a,b,c));
return 0;
}
It just need some sanity checking, its working now:
#include<stdio.h>
int rootnumber(double a, double b, double c);
int main ()
{
double a, b, c, root1,root2;
int rootnum;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf",&a, &b, &c);
rootnum=rootnumber(a,b,c);
printf("the number of roots for this equation is %d", rootnum);
return 0;
}
int rootnumber(double a, double b, double c)
{
int formula= (b*b) - (4*a*c);
if (formula<0)
return 0;
if (formula==0)
return 1;
else
return 2;
}

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.

Integration by Simpson's Rule

One of my assignments was to create a c program that uses the Simpson's 1/3 rule to find the sum. I am running into issues that I am having trouble fixing. Can some one with more experience point me in the right direction?
In theory my code integrates y=ax^2+bx+c where the user selects values for a,b,c and then the user selects the upper and lower bounds [d,e]. Then the user selects the n value which splits up the area into more rectangles (the value that we will use in my class is 100, so the area is split into 100 rectangles). After which it runs through the Simpson's rule and prints out the sum.
//n is required number of iterations.
#include<stdio.h>
#include<conio.h>
#include<math.h>
double integral (int a,int b,int c,int d,int e,int n)
int main()
{
double a, b, c, d, e, n;
printf("Please select values for y=ax^2+bx+c");
printf("Please select value for a");
scanf("%d", &a);
printf("Please select value for b");
scanf("%d", &b);
printf("Please select value for c");
scanf("%d", &c);
printf("Please select value for the upper limit");
scanf("%d", &d);
printf("Please select value for the lower limit");
scanf("%d", &e);
printf("Please select the number of rectangles for the Simpson's Rule (Input 100)");
scanf("%n", &n);
int i;
double sum=0,length=(double)(d-e)/(n),ad,bd,cd,dd;
ad=(double)a;
bd=(double)b;
cd=(double)c;
dd=(double)d;
for (i=0;i<n;i++)
{
sum+=(ad*(dd*dd+2*dd*length*i+length*length*i*i)+bd*(dd+length*i)+cd)*length;
printf("the value is = %d",sum);
}
return sum;
}
Why do you think this
scanf("%e", &e);
should be that way?
The scanf() function takes a format specifier to match the scanned input with, in your case you want to store the values in a double variable, for which you need the "%lf" specifier, so all your scanf()'s should change to
scanf("%lf", &whateverDoubleVariableYouWantToStoreTheResultIn);
You don't need to cast from a variable of a given type to the same type, like here
dd=(double)d;
And also, you must know, that scanf() returns a value, you should not ignore it because your program will misbehave in case of bad input, you should check scanf() in a library manual or the C standard to understand better how to use it.
In addition to #iharob fine advice:
Change n type
// double a, b, c, d, e, n;
double a, b, c, d, e;
int n;
Adjust input code
// and previous lines
if (1 != scanf("%lf", &e)) // %d --> %lf
Handle_InputError();
printf("Please select the number of rectangles for the Simpson's ...
if (1 != scanf("%d", &n) // %n --> %d
Handle_InputError();
Adjust output
// printf("the value is = %d",sum);
printf("the value is = %e",sum); // or %f
Minor bits
// int main()
int main(void) // or int main(int argc, char *argv[])
// return sum; returning a double here is odd
return 0;

Resources