I was solving a code of maximum and minimum using functions. I wrote the code like this:
#include <stdio.h>
int maxmin(int x, int y);
int main () {
int a, b, c;
scanf("%d%d", &a, &b);
c = maxmin(a, b);
if (maxmin == 1) {
printf("%d is maximum,%d is minimum", a, b);
}
else
printf("%d is maximum,%d is minimum", b, a);
return 0;
}
int maxmin(int x, int y) {
int XisMax = 0;
if (x > y) {
XisMax=1;
}
else {
XisMax=0;
}
return XisMax;
}
So my output shows this results:
Input:9,10;
10 is maximum,9 is minimum
Input:10,9;
9 is maximum,10 is minimum
What is the mistake here? What should I do?
PS:I have an exam on functions so solutions using functions will be helpful.
if (maxmin==1)
change toif (c==1)
your problem is solve.
Have a Good day
You should be checking if(c == 1), not if(maxmin == 1).
Your function can also be made shorter:
int maxmin(int x, int y)
{
if (x>y)
{
return 1;
}
else
{
return 0;
}
}
Also, I think your scanf is missing a comma between the two %d's.
Related
I need to write two integers (and nothing else) to variables. It does the validation makes sure that none of args is a string and that they are not empty and excepts division by zero, or when i put float as a first arg but when i put float as a second argument it does not makes an exception. How can i solve it using only stdio.h?
#include <stdio.h>
void sum(int a, int b);
void dif(int a, int b);
void prod(int a, int b);
void qut(int a, int b);
int main() {
int x, z;
int digits = scanf("%d %d", &x, &z);
if (digits != 2) {
printf("n/a\n");
return 2;
}
else {
sum(x, z);
dif(x, z);
prod(x, z);
qut(x, z);
}
return 0;
}
void sum(int a, int b) {
printf("%d ", a + b);
}
void dif(int a, int b) {
printf("%d ", a - b);
}
void prod(int a, int b) {
printf("%d ", a * b);
}
void qut(int a, int b) {
if (a == 0 || b == 0) {
printf("n/a\n");
}
else {
printf("%d\n", a / b);
}
}
Sorry, i understand that the code is quiet simple and my question is quiet dumb :)
Thx!
As mentioned in the comments, scanf is the WRONG TOOL for this job. scanf is notoriously bad at error handling.
Theoretically it's possible — barely possible — to solve this problem using scanf. By the same token, it's possible to drive a screw into a piece of wood using a hammer. But it's a terrible idea. A woodshop teacher who taught his students to drive screws using a hammer would be fired for incompetence. But for some reason we tolerate this kind of incompetence in teachers of beginning programming.
Normally I don't do homework problems here; normally that's a bad idea, too; normally it makes much more sense to have you, the student, do the work and acquire the learning. In the case of boneheaded assignments like this one, though, I have no qualms about giving you a fully-worked-out solution, so you can get your incompetent instructor off your back and go on to learn something more useful. Here is the basic idea:
Read a line of text (a full line), using fgets.
Parse that line using sscanf, ensuring that it contains a number and a number, and nothing else.
Specifically, we'll use %d to read the first integer, and %d to read the second integer, and then we'll use a third %c to pick up whatever character comes next. If that character is anything other than the \n that marks the end of the line, it indicates that the user has typed something wrong, like a string, or the . that's part of a floating-point number.
This is basically the same as user3121023's solution.
int main()
{
char line[100];
int x, z;
char dummy;
if(fgets(line, sizeof(line), stdin) == NULL) return 1;
int digits = sscanf(line, "%d%d%c", &x, &z, &dummy);
if(digits < 2 || digits > 2 && dummy != '\n') {
printf("n/a\n");
return 2;
}
...
See also What can I use for input conversion instead of scanf?
Footnote: The code here has one unfortunate little glitch: If the user types a space after the second number, but before the newline, the code will reject it with n/a. There are ways to fix that, but in my opinion, for this exercise, they're just not worth it; they fall under the "law of diminishing returns". If your users complain, just act like incorrigible software vendors everywhere: remind them that they were supposed to type two numbers and nothing else, and the space they typed after the second number is "something else", so it's THEIR FAULT, and not your bug. :-)
After scanning for the integers, use a loop to scan for one whitespace character. Break out of the loop on a newline.
For any other character, the scan will return 0.
#include <stdio.h>
void sum(int a, int b);
void dif(int a, int b);
void prod(int a, int b);
void qut(int a, int b);
int main() {
char ws[2] = ""; // whitespace
int scanned = 0;
int x, z;
int digits = scanf("%d %d", &x, &z);
if (digits != 2) {
printf("n/a\n");
return 2;
}
while ( ( scanned = scanf( "%1[ \t\n]", ws))) { // scan for 1 whitespace
if ( scanned == EOF) {
fprintf ( stderr, "EOF\n");
return 1;
}
if ( ws[0] == '\n') {
break;
}
}
if ( scanned != 1 || ws[0] != '\n') {
printf("n/a\n");
return 3;
}
else {
sum(x, z);
dif(x, z);
prod(x, z);
qut(x, z);
}
return 0;
}
void sum(int a, int b) {
printf("%d ", a + b);
}
void dif(int a, int b) {
printf("%d ", a - b);
}
void prod(int a, int b) {
printf("%d ", a * b);
}
void qut(int a, int b) {
if (a == 0 || b == 0) {
printf("n/a\n");
}
else {
printf("%d\n", a / b);
}
}
I am first time poster here. Like the tittle says I need to print all odd numbers via a recursive function. The problem is that I have created a simple program that does that, but when it reaches 1(which should be the point where the program stops) the program crashes and I honestly do not see where is the problem. My professor said that I forgot to put a return somewhere, but I honestly do not know where. So if someones can point out the problem that would be great(ps. I am using Code::Blocks as my IDE).
int main() {
int a, b;
printf("Unesi neki broj: \n");
scanf("%d", &a);
b = koko(a);
printf(b);
}
int koko(int a) {
if (a == 1) {
return a;
}
if (a % 2 != 0) {
printf("Ovaj broj je neparan: %d \n", a);
}
koko(a - 1);
}
First, you have to declare the function koko; for using it in main function.
int koko(int a);
Secondly, printf(b) need to define the type to print out:
printf("%d\n", b);
Finally, Using return koko(a - 1); instead of koko(a-1) because this function has to return an int value.
Then, the complete code:
#include <stdio.h>
#include <stdlib.h>
int koko(int a);
int main() {
int a, b;
printf("Unesi neki broj: \n");
scanf("%d", &a);
b = koko(a);
printf("%d\n", b);
return 0;
}
int koko(int a) {
if (a == 1) {
return a;
}
if (a % 2 != 0) {
printf("Ovaj broj je neparan: %d \n", a);
}
return koko(a - 1);
}
enter image description here
int koko(int a)
{
if (a % 2 != 0)
{
printf("Ovaj broj je neparan: %d \n", a);
}
if (a == 1) {
return a;
}
koko(a - 1);
}
int main()
{
int a, b;
printf("Unesi neki broj: \n");
scanf("%d", &a);
b = koko(a);
printf(" koko(a) return %d", b);
return 0;
}
I need to write a function to compute a^b but I am not allowed to use pow. Any ideas? I am lost.
It looks like problem is in main now...
Somewhere it gets that vys is what i characterise it. So if i set that vys=1 in main i get 1 in output..
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <unistd.h>
void multiplied(int b, int n)
{
int i=1, vys=1;
while (i<=n)
{
vys *=b;
i++;
}
return vys;
}
main(void)
{
int b=0, n=0, vys=1;
printf("Give numbers b and n but they must be in interval <0,10>!\n");
scanf("%d %d", &b, &n);
if ((b < 0 || b>10) || (n<0 || n>10))
{
printf("Numbers are not in interval <0,10>!\n");
}
else
{
printf("Number is in interval so i continue...\n");
sleep(2);
vys= multiplied(&b, &n);
printf("%d", vys);
}
Let's be explicit.
First, this
void multiplied(int *b, int *n)
returns an int, so say so.
int multiplied(int *b, int *n)
Next, you initialised variables in main: do the same here.
int i, vys;
Like this:
int i=1, vys=1;
Now let's look at the loop:
while (i<=n)
{
vys=*b**b;
i++;
}
As it stands, you are setting vys to something over and over again in the loop.
You want to multiply up, e.g. 2, then 2*2, then 2*2*2, .... if you want a power of two:
while (i<=n)
{
vys *= *b;
i++;
}
Now, you don't need to pass pointers.
int multiplied(int b, int n)
{
int i=1, vys=1;
while (i<=n)
{
vys *= b;
i++;
}
return vys;
}
Edit:
Watch out for when you call the function:
main(void)
{
int b=0, n=0, vys;
//input and checking code as you have it
multiplied(&b, &n); //<---- return ignored
printf("%d", vys); //<-- print uninitialsed local variable
}
Change you last two lines:
vys = multiplied(&b, &n); //<---- return captured
printf("%d", vys); //<-- print returned variable
Edit 2:
With the change to use int in the function and not pointers, pass the ints not their addresses:
vys = multiplied(b, n); //<---- pass the ints not their addresses
printf("%d", vys); //<-- print returned variable, which should vary now
Here you have a simple code:
#include <stdio.h>
long long intpow(int a, int b)
{
long long tempres = 1;
while(b--)
tempres *= a;
return tempres;
}
int main(void) {
printf("%lld\n", intpow(5,10));
return 0;
}
you need much larger int to accommodate the result.
You can play with it yourself: https://ideone.com/4JT6NQ
Ok so I have written this code with four different functions, and the main purpose of it is to display in a table from angles 0-90 what the angle, time, distance of a velocity is. the velocity is inputed from the user.
But when I call the void function that is making the function I get an error "undefined reference to `create_table'" Here is my code.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define G 9.8 /* gravitation acceleration 9.8 m/s^2 */
#define PI 3.141592654
void create_table(double v);
double Projectile_travel_time(double a, double v);
double Projectile_travel_distance(double a, double v);
double degree_to_radian(double d);
int main(void)
{
int n;
double velocity;
printf ("please enter the velocity at which the projectile is launched (m/sec): ");
n = scanf("%lf" ,&velocity);
if(n != 1)
{
printf("Invlid input. Bye...");
exit(1);
}
while (velocity < 0 )
{
printf ("please enter a positive number for velocity: ");
n = scanf("%lf", &velocity);
if(n != 1)
{
printf("Invlid input. Bye...");
exit(1);
}
}
create_table(velocity);
return 0;
}
void create_table(double v)
{
printf("Angle t d\n");
printf("(deg) (sec) (m)\n");
double a,i;
for( a=0; a<=90; a+=5)
{
for(i=0; i<=2; i++)
{
double t = Projectile_travel_time(a, v);
double s = Projectile_travel_distance(a, v);
printf("%d %d %d\n", a, t, s);
}
}
}
double Projectile_travel_time(double a, double v)
{
double t = ((2*v*sin(degree_to_radian(a)))/(G));
return t;
}
double Projectile_travel_distance(double a, double v)
{
double d = ((v*v)/G)*sin(2*degree_to_radian(a));
return d;
}
double degree_to_radian(double d)
{
double r = d*atan(1) * 4 / 180;
return r;
}
any help would be appreciated.
thanks
edit I have edited the code but now have encountered another problem with my outputs being completely off. Any suggestions how my functions are incorrect?
You must keep the functions you create outside of the main function
Try to implement your functions outside the main()
You need to move the function definitions out of main. C does not support nested functions.
Edit: That is, in GCC they are, but it's not portable.
I have edited the code but now have encountered another problem with my outputs being completely off.
Change
printf("%d %d %d\n", a, t, s);
to
printf("%lf %lf %lf\n", a, t, s);
You can use %7.3f to align all the values.
Write all the function definitions create_table, Projectile_travel_time, Projectile_travel_distance and degree_to_radian outside the main.
Linker is not able to find the definition of create_table at the point at which you are calling create_table.
I am learning recursion and i encountered a conceptual doubt while solving the problem of calculation of remainder when a positive integer is a is divided by a positive integer b.
My code is:
#include<stdio.h>
#include<stdlib.h>
int x;
int rem(int a,int b)
{
x=a;
if(x>=b)
{
x=x-b;
rem(x,b);
}
printf("%d\n",x);
return x;
}
int main()
{
int a,b;
printf("Enter a & b\n");
scanf("%d %d",&a,&b);
int y =rem(a,b);
printf("rem is :%d",y);
return 0;
}
Its working fine. I have learned that for every call a new set of formal parameters and local variables are created.
So i experimented it by printing x on return of every recursive call!
But it is printing 1 1 1 1. Why is the value of x corresponding to a particular call not printed. ?
Why only the last modified value printed?.. Is that because i declared 'x' as global?
In this case perhaps you need only to move your print up
int rem(int a,int b)
{
x=a;
printf("%d\n",x);
if(x>=b)
{
x=x-b;
rem(x,b);
}
return x;
}
But I think you should avoid the use of global variables in a recursive alrotithm. It could make the algorithm very difficult to reason about. Recursive functions are better to be 'pure functions'.
It is because while x >= b, rem() is repeatedly called before printf()s are called. Only after x < b will the printf()s are called as each call on rem() unwinds.
You might want to make x local to rem() to get the desired result.
Ignoring issues with checking the return value from scanf() and that the two entered values are both positive, etc, I think you can and should avoid x altogether. You could use:
#include <stdio.h>
static int rem(int a, int b)
{
if (a >= b)
a = rem(a-b, b);
printf("%d\n", a);
return a;
}
int main(void)
{
int a, b;
printf("Enter a & b\n");
scanf("%d %d", &a, &b);
int y = rem(a, b);
printf("rem(%d, %d) is: %d\n", a, b, y);
return 0;
}
This code captures the return value from rem() at each level of recursion. In this case, because the returned value doesn't change as the recursion unwinds, you could use the global variable x, but there is no need for it, and you should avoid global variables whenever you can.
#include<stdio.h>
#include<conio.h>
int fun(int,int);
int main()
{
int a,b;
printf("enter two numbers");
scanf("%d %d",&a,&b);
fun(a,b);
//printf("%d",fun(a,b));
}
int fun(int a,int b)
{
if(a<b)
printf("%d",a);
if(a>=b)
a=fun(a-b,b);
return a;
}