using srand to insert random numbers - c

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

Related

How to display and add all even numbers?

How can I display and add all even numbers? The current code displays numbers between 2 numbers in an ascending manner.
#include <stdio.h>
main() {
int a;
int b;
printf("Enter integer a:");
scanf("%d", &a);
printf("Enter integer b:");
scanf("%d", &b);
if(b > a)
{
do {
printf("Result: %d\n", b);
b--;
} while (a <= b);
}
else
{
do {
printf("Result: %d\n", a);
a--;
} while (a >= b);
}
}
To check if an integer is even you can check if the least significant bit is zero.
To check if an integer is odd you can check if the least significant bit is one.
You can do that using bitwise AND (&).
Something like:
if(b > a)
{
if (b & 1) b--; // Make b even
if (a & 1) a++; // Make a even
int sum = 0;
do
{
sum += b;
printf("b is %d, sum is %d\n", b, sum);
b = b - 2;
} while (b >= a);
}
All even numbers are divisible by 2. You need to check if the remainder of division by 2 is equal to zero. In order to do it, you can use the modulo operator (%).
To display only even numbers:
if ((b%2) == 0) {
printf("Result: %d\n", b);
}
I'd write a function which I could test for different possible combinations of the extremes:
#include <stdio.h>
void evens(int a, int b)
{
// Make sure to always start from the greatest value.
if ( a > b ) {
int tmp = a;
a = b;
b = tmp;
}
// Make sure to always start from an EVEN value.
if ( b % 2 != 0 ) {
--b;
}
int sum = 0;
while ( a <= b ) {
printf("%d ", b);
sum += b;
b -= 2; // Jump directly to the previous even number.
}
printf("\nSum: %d\n", sum);
}
int main(void)
{
// Those should all print "10 8 6 4 2 \nSum: 30\n"
evens(1, 10);
evens(10, 1);
evens(2, 11);
evens(11, 1);
evens(2, 10);
}

What should I fix to create random calculation in this code?

I wanted to fix my code to create random operation here, what should I fix here?
#include <stdio.h>
int main()
{
int x,y,z,a;
char o;
while(1)
{
printf("give your three numbers: ");
scanf("%d %d %d",&x,&y,&z);
printf("(%d*%d-%d) what is the correct among following answers? \n 1. %d\n 2. %d\n 3. %d\n ",
x,y,z,x*y-z,x*y*z,x-y*z);
printf("what is answer? \n: ");
scanf("%d",&a);
if(a == 1)
{
printf("you are right!\n");
}
else
{
printf("you are false!\n\n");
}
getchar();
printf("Would you like to exit programm? (y/n) : ");
scanf("%c",&o);
if (o=='Y' || o=='y')
{
break;
}
else if(o=='N' || o=='n')
{
continue;
}
else
{
printf("Wrong input!!!!");
}
return 0;
}
}
What I mean is I want to try change operation such as * + - randomly when I run code, and also along with this question, the answer should be changed...
thank you!
Your question is mainly focused on shuffling the answers. By studying from couple of sites I have found an solution for this. This is little bit difficult but studying the code few times you can get it
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void randomize(int arr[], int n) {
srand(time(NULL));
int i;
for(i = n-1; i > 0; i--) {
int j = rand() % (i+1);
swap(&arr[i], &arr[j]);
}
}
int main() {
int x,y,z,a;
int i;
int a1, a2, a3;
int arr[] = {1, 2, 3};
int n = sizeof(arr)/ sizeof(arr[0]);
printf("give your three numbers: ");
scanf("%d %d %d",&x,&y,&z);
a1 = x*y-z;
a2 = x*y*z;
a3 = x-y*z;
printf("Before shuffle = %d %d %d\n\n", a1, a2, a3);
char answers[] = {a1, a2, a3};
printf("(%d*%d-%d)what is the correct among following answers?\n", x, y, z);
randomize (arr, n);
for(i=0; i<n; i++) {
int index = arr[i];
printf("%2d - %d\n", i+1, answers[index]);
}
return 0;
}
This is the outputs
First time run
give your three numbers: 2
2
2
Before shuffle = 2 8 -2
(2*2-2)what is the correct among following answers?
1 - 8
2 - 2
3 - -2
Second time run
give your three numbers: 2
2
2
Before shuffle = 2 8 -2
(2*2-2)what is the correct among following answers?
1 - -2
2 - 8
3 - 2
You can try this multiple times. Every time answers will be shuffle. This is the Source I have referred.
Just provide one of examples here. There are many kinds way to implement. In the random operation generation part. I would like to write a random number generator function, which purpose is to generate the given range of random number.
int gen_random(int min, int max)
{
srand( time(NULL) );
return rand() % (max - min + 1) + min;
}
In order to represent four operator (+, -, *, /), The number (let's call the operation number) 1 to 4 is used for represent four operator respectively. The calculation function map the operation number to operation and get the result.
int calculation(int number1, int number2, int op)
{
switch(op)
{
case 1:
return number1 + number2;
case 2:
return number1 - number2;
case 3:
return number1 * number2;
case 4:
return number1 / number2;
}
}
You can use the gen_random and the calculation function above to change operation randomly. The usage of the gen_random and the calculation function is something like:
int op = gen_random(1, 4); // Generate operation randomly
printf("%d\n", op);
printf("%d\n", calculation(x, y, op)); // Pass x, y, and operation number into calculation function to get answer

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

C Newbie/ not understanding what might the problem be

since im learning C language i decided to make a simple program that adds, substracts and calculates the product of two variables . Depending on users' input whether its 1,2 or 3 to choose addition/substraction/folding.
#include <stdio.h>
int main (void) {
int a, b, c, d, e, f, g;
a=19; b=11; c=a+b; d=a-b; e=a*b; f=-1;
while (f<0 && f>3) {
printf("-press 1 to calculate the sum of a and b\n");
printf("-press 2 to calculate the difference between a and b\n");
printf("-press 3 to calculate the product of a and b\n");
scanf("%d\n",&g);
f=g;
return;
}
if (f == 1) {
printf("A+B= %ls\n", &c);
} else if (f == 2) {
printf("A-B= %ls\n", &d);
} else if (f == 3) {
printf(" A*B= %ls\n", &e);
}
return 0;
}
When i run the program its reads "g" and then it stops.
any suggestions to why is this happening
btw i also tried removing the while statement.
I think you mean the following while loop
while ( f < 0 || f > 3 )
{
printf("-press 1 to calculate the sum of a and b\n");
printf("-press 2 to calculate the difference between a and b\n");
printf("-press 3 to calculate the product of a and b\n");
scanf( "%d", &g );
f = g;
}
That is pay attention to 1) the condition of the while loop, 2) removed the return statement and 3) removed the character '\n' in scanf because it is redundant.
And in printf calls remove the operator & and use the conversion specifier %d.
if ( f == 1 ) {
printf(" A+B= %d\n", c) ;
}
else if ( f == 2) {
printf(" A-B= %d\n", d );
}
else if ( f == 3 ) {
printf(" A*B= %d\n",e );
}
I fixed you code and added some comments to it. Hope you can understand it! If you have questions, just comment them.
#include <stdio.h>
int main (void) {
// values can be given to ints by int a = value
int a = 19, b = 11, c, d, e, f = -1, g;
// '||' means OR, while '&&' means AND.
// A value can't be smaller then and bigger then 3 at the same time
while (f < 0 || f > 3) {
printf("-press 1 to calculate the sum of a and b\n");
printf("-press 2 to calculate the difference between a and b\n");
printf("-press 3 to calculate the product of a and b\n");
scanf("%d", &g); //you don't need \n for scanf
f=g;
// return stops the program. In this case, this is not what you want
}
// For printf you don't need &e, &f, etc.
// Instead of calculating a value and storing it in a different int,
// you can just print the outcome of the calculation.
if (f == 1) {
printf("A + B = %d\n", a + b);
} else if (f == 2) {
printf("A - B = %d\n", a - b);
} else if (f == 3) {
printf(" A * B = %d\n", a * b);
}
return 0;
}
Try this:
while (f<0 && f>3){
printf("-press 1 to calculate the sum of a and b\n");
printf("-press 2 to calculate the difference between a and b\n");
printf("-press 3 to calculate the product of a and b\n");
scanf("%d\n",&g);
f=g;
// remove the "return" from here;
}
Also change :
printf(" A+B= %ls\n",&c);
To:
printf(" A+B= %d\n",c);
You don't need the '&' operator to print values, if used it will print the address and not the resultant value.
I would like to add that you could replace your if/else if statements with the switch/case . To be more specific (after changing some small mistakes in your code %d instead %ls and so on) the section of your code should look like this:
// code above
switch(f) {
case 1:
printf(" A+B= %d\n", c) ;
break; // if you want the program to stop after doing this operation
case 2:
printf(" A-B= %d\n", d );
break;
case 3:
printf(" A*B= %d\n",e );
break;
// default: // if the input is not one of the above(1,2 or 3)
// printf(" Please give a valid option. \n");
// break;

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