do while statement in C - c

#include <stdio.h>
int main()
{
char a;
int b,c,d,e;
int i =0;
do {
printf("Enter pet size (s/m/l): ");
scanf("%s", a);
printf("Enter number of days: ");
scanf("%d", b);
c = b * 8;
d = b * 11;
e = b * 14;
if ( a = 's')
{
printf("Kennelling cost: %d\n", c);
}
else if ( a = 'm')
{
printf("Kennelling cost: %d\n", d);
}
else
{
printf("Kenneling cost: %d\n", e);
}
} while (i =0);
return 0;
}
When trying to enter number of days it completely skips the statement and automatically multiplies with random number. i cant see to know what the problem is the syntax seems to be right as far as i know .

'=' is completely different from '=='. by doing a='s' or i=0 you are assigning the value to the variable.
Please use '==' for condition validation.

You are using the assignment operator = in your boolean expressions instead of == to make a comparison, which is grammatically valid, but most likely not what you intended.

Corrected version
#include <stdio.h>
int main () {
char a;
int b,c,d,e;
int i =0;
do {
printf("Enter pet size (s/m/l): ");
scanf("%s", &a);
printf("Enter number of days: ");
scanf("%d", &b);
c = b * 8;
d = b * 11;
e = b * 14;
if ( a == 's')
{
printf("Kennelling cost: %d\n", c);
}
else if ( a == 'm'){
printf("Kennelling cost: %d\n", d);
}
else
{
printf("Kenneling cost: %d\n", e);
}
} while (i ==0);
}
scanf statements not having addressof(&) operator
comparison(==) operators needed to be used in if statements instead of assignment(=) operators.

Related

Write a recursive function in C that prints all odd numbers(backwards) and when it reaches 1 it stops

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;
}

Why does my code jump to else immediately?

#include <stdio.h>
int printMenu(int);
int studentglobal;
float getCarryMark(float);
float cm1;
main()
{
printf("-----------------------------------------------------\n");
printf("\t\tTotal Score calculator\n");
printf("-----------------------------------------------------\n");
int counter, x, studentcount = 1,sum = 0 ;
x = printMenu(studentglobal);
for (counter = 0; counter < x; counter++)
{
studentcount = studentcount + counter;
printf("Student : %d \n", studentcount);
getCarryMark(cm1);
if (cm1 >= 0 && cm1 <= 50)
{
printf("right range!!\n");
}
else
{
printf("INVALID RANGE!!!\n");
}
printf("%.2f\n", cm1);
}
}
int printMenu(int nstudent)
{
printf("Enter no of student: ");
scanf("%d", &nstudent);
return(nstudent);
}
float getCarryMark(float carrymark)
{
printf("Enter your carrymarks: ");
scanf("%f", &carrymark);
return(carrymark);
}
So actually when I enter 200, it shows INVALID RANGE!!!, but when I enter 20 it still shows INVALID RANGE!!!. It somehow skipped the if statement. Please don't bother the other part, if I have any mistake tell me please. ert gf dfg dgd dg dfgd gd dg dg dgdfg
You need to return carrymark from getCarryMark:
float getCarryMark(float carrymark)
{
printf("Enter your carrymarks: ");
scanf("%f", &carrymark);
return(carrymark);
}
You are missing a return statement in getCarryMarks method !
You missed the return statement in getCarryMark
getCarryMark function takes a parameter by value, modifies the value and returns it back, however, the returned value is never used. Modifying the parameter's value does not reflect this change to the outside since it has been passed by value.
I have partially updated the code so that it could execute the if statement. Please try the following code.
#include <stdio.h>
int printMenu(int);
int studentglobal;
float getCarryMark(float);
float cm1;
main()
{
printf("-----------------------------------------------------\n");
printf("\t\tTotal Score calculator\n");
printf("-----------------------------------------------------\n");
int counter, x, studentcount = 1,sum = 0 ;
x = printMenu(studentglobal);
for (counter = 0; counter < x; counter++)
{
studentcount = studentcount + counter;
printf("Student : %d \n", studentcount);
cm1 = getCarryMark();
if (cm1 >= 0 && cm1 <= 50)
{
printf("right range!!\n");
}
else
{
printf("INVALID RANGE!!!\n");
}
printf("%.2f\n", cm1);
}
}
int printMenu(int nstudent)
{
printf("Enter no of student: ");
scanf("%d", &nstudent);
return(nstudent);
}
float getCarryMark()
{
float carrymark = 0.0;
printf("Enter your carrymarks: ");
scanf("%f", &carrymark);
return(carrymark);
}

Fibonacci Series in C Program where FIRST 2 numbers are given by user

When the first number is 1 and second number is 2, and the length is 5, it should be 1 2 3 5 8. But then my output is always 1 2 1 3 4. I can't seem to find the problem.
Another input is 2 and 5. Output is 2 5 1 6 7. The 3rd number which is 1 shouldn't be there. What should I change or add?
*This is already a submitted HW and yes its wrong I got the deductions already. Now I just want to fix this so I can study this.
int main()
{
int i, lenght = 0, fib, sum, sum1, sum2, a, b, c;
printf("\nFirst number: ");
scanf("%d", &a);
printf("\nSecond number: ");
scanf("%d", &b);
printf("\nHow long?: ");
scanf("%d", &lenght);
{
while ((a > b) || ((lenght < 2) || (lenght > 100)))
{
printf("\nFirst number: ");
scanf("%d", &a);
printf("\nSecond number: ");
scanf("%d", &b);
printf("\nHow long?: ");
scanf("%d", &lenght);
}
}
printf("%d\t%d\t", a, b);
printf("%d\t", fib);
for (i = 3; i < lenght; i++) {
if (i <= 1) fib = i;
else {
a = b;
b = fib;
fib = a + b;
}
printf("%d\t", fib);
}
}
The first time you print fib (before the for loop), you haven't assigned it anything yet.
Since this is for study, issues with your code: you don't need to duplicate the calls to scanf(), simply initialize one of the variables to fail (which you did: lenght = 0) and let the loop do its thing; pick one indentation style and stick with it; if you're new to C, always include the curly braces, even when the language says they're optional; you (correctly) allow for a length of 2, but then print three numbers; your if (i <= 1) clause is a no-op as the loop starts with for (i = 3; so i is never less than 3.
Putting it all together, we get something like:
#include <stdio.h>
int main() {
int length = 0, a, b;
while (length < 2 || length > 100 || a > b ) {
printf("\nFirst number: ");
(void) scanf("%d", &a);
printf("\nSecond number: ");
(void) scanf("%d", &b);
printf("\nHow long?: ");
(void) scanf("%d", &length);
}
printf("%d\t%d\t", a, b);
for (int i = 2; i < length; i++) {
int fib = a + b;
printf("%d\t", fib);
a = b;
b = fib;
}
printf("\n");
return 0;
}
Note that the input error checking isn't sufficient to prevent problems. E.g. b can be greater than a, but still mess up the sequence if you input random numbers. You're assuming the user knows to put in two adjacent items from fibonacci sequence which is tricky to test.
just add fib=a+b; before printing fib value.
Its a good coding habit to initialize all variable before using it(especially in C).
Your code seems strange to me. I tried to simplify your code a bit. See this once:
int main()
{
int a,b,next,last,i;
printf("Enter the first Value:");
scanf("%d",&a);
printf("Enter the second Value:");
scanf("%d",&b);
printf("Enter the length of Fab. series:");
scanf("%d",&last);
printf("%d,%d,",a,b);
for (i=3; i<= last; i++)
{
next = a + b;
if(i<last)
printf("%d,",next);
else
printf("%d",next);
a = b;
b = next;
}
return 0;
}
Hope it's Helpful!!

using srand to insert random numbers

I am having trouble with my syntax for calling srand to insert random numbers for an addition problem?? tried looking up a similar program to view syntax with no luck.
#include<stdio.h>
#include"simpleio.h"
int main()
{
// int seed;scanf("%d",&seed);
// srand((unsigned)seed);
int decision,n,o ;
// scanf("%d", &n);
// srand((unsigned)n);
do{
printf("1.Give me an addition problem.\n"
"2.Give me a subtraction problem.\n"
"3.Give me a multiplication problem.\n"
"4.Quit\n");
scanf("%d", & decision);
} while (decision>5 && decision<0);
if (decision==1)
{
printf("1+1= ");
}
else if (decision==2)
{
printf("1-2\n");
}
else if (decision==3)
{
printf("1*2\n");
}
else if(decision==4)
{
printf("\n");
}
return 0;
}
In similar tasks, it is preferable to do it with switch case :
#include <stdio.h>
#include <string.h>
main()
{
int decision;
do{
printf("1.Give me an addition problem.\n"
"2.Give me a subtraction problem.\n"
"3.Give me a multiplication problem.\n"
"4.Give me a division problem.\n"
"5.Quit\n");
scanf("%d", & decision);
} while (decision >5 || decision <= 0); // correct your condition
// generate two random numbers for the operation
srand(time(NULL));
int a = rand()%20 + 1; // random between 1 and 20
int b = rand()%20 + 1; // random between 1 and 20
switch(decision)
{
case 1: //addition
printf("%d + %d = %d\n", a, b, a+b);
break;
case 2: //subtraction
printf("%d - %d = %d\n", a, b, a-b);
break;
case 3: //multiplication
printf("%d * %d = %d\n", a, b, a*b);
break;
case 4: //division
printf("%d / %d = %d, and remainder = %d\n", a, b, a/b, a%b);
default :
break;
}
return;
}
I included the division as a bonus

Calculating A Determinant From A 2x3 Array Matrix

So I have written the code below as a program to solve a 2-D linear system of equations.
#include <stdio.h>
int main( )
{
int eq[2][3];
int D, Dx, Dy;
int sol[2];
printf("Enter cofficients of first equation: ");
scanf("%d %d %d", &eq[0][0], &eq[0][1], &eq[0][2]);
printf("Enter cofficients of second equation: ");
scanf("%d %d %d", &eq[1][0], &eq[1][1], &eq[1][2]);
D = eq[0][0]*eq[1][1] - eq[1][0]*eq[0][1];
Dx = eq[0][2]*eq[1][1] - eq[1][2]*eq[0][1];
Dy = eq[0][0]*eq[1][2] - eq[1][0]*eq[0][2];
if(D != 0){
sol[0] = Dx/D; // x solution
sol[1] = Dy/D; // y solution
printf("x = %d, y = %d \n", sol[0], sol[1]);
}
else{
printf("No unique solutions exist. \n");
}
return 0;
}
I have now been tasked with converting this into a function using the prototype:
bool determinantFunction(int e[][3], int s[]);
My problem is that I don't know where to start. I have read up on using booleans in C as much as I can but I don't understand how or why I would implement that into making a determinant function.
So, just putting your existing code in such a function (and I'm not saying your code is right or wrong), you get something like:
bool determinantFunction(int e[][3], int s[])
{
int D, Dx, Dy;
// calculate determinant
D = e[0][0]*e[1][1] - e[1][0]*e[0][1];
Dx = e[0][2]*e[1][1] - e[1][2]*e[0][1];
Dy = e[0][0]*e[1][2] - e[1][0]*e[0][2];
// if non-singular ...
if (D != 0)
{
// success
s[0] = Dx/D; // return x solution
s[1] = Dy/D; // return y solution
return true;
}
// no solution
return false;
}
Then your main becomes something like this (not tested):
int main( )
{
int eq[2][3];
int sol[2];
printf("Enter cofficients of first equation: ");
scanf("%d %d %d", &eq[0][0], &eq[0][1], &eq[0][2]);
printf("Enter cofficients of second equation: ");
scanf("%d %d %d", &eq[1][0], &eq[1][1], &eq[1][2]);
if (determinantFunction(eq, sol))
{
printf("x = %d, y = %d \n", sol[0], sol[1]);
}
else{
printf("No unique solutions exist. \n");
}
return 0;
}
For your example : 4x - 3y = -14 and 3x - 5y = -5, which is the same as:
4x - 3y + 14 = 0
3x - 5y + 5 = 0
You'd get:
Ok, last update - hardcoded coefficients:
int eq[2][3] = {{4, -3, 14}, {3, -5, 5}};
int sol[2];
if (determinantFunction(eq, sol))
{
printf("x = %d, y = %d \n", sol[0], sol[1]);
}
else{
printf("No unique solutions exist. \n");
}
One possible way such a function could work is to return true if there was a unique solution and false otherwise. If the solution was indeed found, it would be stored in the array passed as second argument.
Basically, you would just move your existing code into a function. Your sol array will be passed to you as the first argument.
int main()
{
int eq[2][3];
// ...
int sol[2];
if (determinantFunction(eq, sol)) {
printf("%d %d\n", sol[0], sol[1]);
} else {
printf("No unique solutions exist.\n");
}
}

Resources