Check the random number by modulo and user's input - c

im trying to determine how many times the user has guess right the number and place of a random number.
for example, if the number is 1234 and the user's input is 7214
so he guess right only the number 2 and 4.
because of this: 1[2]3[4] == 7[2]1[4].
problem: the program throws me out after i take the user's input. im getting an error which says: "Expression: result_pointer != nullptr"
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
int hit(int num);
int strike(int num);
int rndNum(int num);
void main()
{
int num = 0;
int chosenNum;
int saveHits;
srand(time(NULL));
printf("The Random number: %d", chosenNum = rndNum(num));
printf("\nPlease enter a 4 digit number: ");
scanf("%d", num);
saveHits = hit(num, chosenNum);
printf("\nThe number of hits: %d", saveHits);
getch();
}
int rndNum(int num)
{
int rndNum = rand() % 9000 + 1000;
return rndNum;
}
int hit(int num1, int chosenNum1)
{
int i, hit1 = 0;
for (i = 0; i < 4; i++)
{
if (num1 % 10 == chosenNum1 % 10)
hit1++;
num1 /= 10;
chosenNum1 /= 10;
}
return hit1;
}

You're missing a &
scanf("%d", &num);
^

Related

Why is my program breaking when I add a void function to the main? It works without it

I have 2 functions + my main.
One of them is a void function that "prints out instructions"
The other is the one that actually does what I want it to do.
For some reason when the play function is by itself in the main it works just fine, but as soon as I add the print instructions function, it breaks and I cannot figure out why it's doing that.
Functions:
int playGame();
void printInstructions();
`
int playGame()
{
int dice[100];
int diceAmount, j, sum = 0;
printf("How many dice would you like to roll? ");
scanf("%d",&diceAmount);
for( int i = 0; i < diceAmount; i++)
{
dice[j] = rand() % 6 + 1;
sum += dice[j];
printf("Dice %d: %d\n",i+1,dice[j]);
}
printf("---------\nSum: %d", sum);
}
`
`
void printInstructions()
{
printf("--------------\n");
printf("- HOW TO WIN -\n");
printf("--------------\n");
printf("Your dice roll must equal 7 or 11 or else you lose.\n");
printf("Want to test your luck?\n\n");
}
`
Whole thing:
`
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int playGame();
void printInstructions();
int playGame()
{
int dice[100];
int diceAmount, j, sum = 0;
printf("How many dice would you like to roll? ");
scanf("%d",&diceAmount);
for( int i = 0; i < diceAmount; i++)
{
dice[j] = rand() % 6 + 1;
sum += dice[j];
printf("Dice %d: %d\n",i+1,dice[j]);
}
printf("---------\nSum: %d", sum);
}
int main()
{
printInstructions();
playGame();
}
void printInstructions()
{
printf("--------------\n");
printf("- HOW TO WIN -\n");
printf("--------------\n");
printf("Your dice roll must equal 7 or 11 or else you lose.\n");
printf("Want to test your luck?\n\n");
}
`
Without the printInstructions();
With the printIUnstruction();
Why is it breaking?
With the suggestions from UnholySheep & Martin James, I was able to get my code to work.
Here is the working code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int playGame();
void printInstructions()
{
printf("--------------\n");
printf("- HOW TO WIN -\n");
printf("--------------\n");
printf("Your dice roll must equal 7 or 11 or else you lose.\n");
printf("Want to test your luck?\n\n");
}
int playGame()
{
int dice;
int diceAmount, sum = 0;
printf("How many dice would you like to roll? ");
scanf("%d",&diceAmount);
for( int i = 0; i < diceAmount; i++)
{
dice = rand() % 6 + 1;
sum += dice;
printf("Dice %d: %d\n",i+1,dice);
}
printf("---------\nSum: %d\n",sum);
}
int main()
{
printInstructions();
playGame();
}
Result:

Function to find odd numbers returns extra value - C Language

I wrote a code in C to find the odd numbers from a given interval of min and max number. The function works well when it is inside the int main() but not well when outside the program as a function.
What's more is that it also prints the incremented number outside the max number given.
This is the code...
#include <stdio.h>
// My Function
int odd_numbers(int x, int y) {
for (int i = x; i <= y; ++i) {
if (i % 2 == 1) {
printf("%d\n",i);
}
}
}
// Main Program
int main(void) {
int min_num, max_num;
printf("Input your minimum number: ");
scanf("%d", &min_num);
printf("Input your maximum number: ");
scanf("%d", &max_num);
printf("%d",odd_numbers(min_num,max_num));
}
and this is the output...
As you can see, it adds an 11 besides the 9...
How can I solve this? I've tried return 0; and it returns the value 0 but i only want to return no number except the odd numbers.
Here is the working code.
Notes
Change the return type of odd_numbers from int to void because you are not returning anything when the function is called.
Only call the function odd_numbers, no need to printf anything because odd_numbers already does the job.
#include <stdio.h>
// My Function
void odd_numbers(int x, int y) {
for (int i = x; i <= y; i++) {
if (i % 2 != 0) {
printf("\n%d",i);
}
}
}
// Main Program
int main(void) {
int min_num, max_num;
printf("Input your minimum number: ");
scanf("%d", &min_num);
printf("Input your maximum number: ");
scanf("%d", &max_num);
odd_numbers(min_num,max_num);
}
Here is the modified code.
you have declare function return type int but return nothing. odd_numbers made to void type. no need to return anything
code:
#include <stdio.h>
// My Function
void odd_numbers(int x, int y)
{
int i = 0;
for (int i = x; i <= y; i++)
{
if (i % 2 != 0)
{
printf("%d\n", i);
}
}
}
// Main Program
int main(void) {
int min_num, max_num;
printf("Input your minimum number: ");
scanf("%d", &min_num);
printf("Input your maximum number: ");
scanf("%d", &max_num);
odd_numbers(min_num, max_num);
return 0;
}

Sum of digits of some number

for example, if I typed in 12345 then sum = 1+2+3+4+5
Using C
I used some kind of approach that I'm not sure about it and there is a problem with the line referred to down in the code
int i ;
int sum;
int individual;
int n;
printf("enter the number: ");
scanf("%d",&n);
for (i=0;i<5;++i){
**indvidual=+n[i];**
sum=+invidual;
}
printf("%d",sum)
return 0;
You can divide by 10 and see the remainder to obtain each digits.
Also you should use +=, not =+, to add things to variable and initialize the variables before using their values.
int i ;
int sum = 0; /* initialize */
int individual;
int n;
printf("enter the number: ");
scanf("%d",&n);
for (i=0;i<5;++i){
indvidual=+(n%10); /* obtain a digit (+ is not required, but left by respect) */
n/=10; /* eliminate last dight and proceed to next digit */
sum+=invidual; /* use += instead of =+ */
}
printf("%d",sum); /* also add semicolon here */
return 0;
#include <stdlib.h>
#include <stdio.h>
int main()
{
int i = 12345;
int sum;
i = abs(i);
sum = i?i%9?i%9:9:0;
printf ("Sum of digits is %d\n", sum);
}
Result is 6
Because 1 + 2 + 3 + 4 + 5 = 15
and then 1 + 5 = 6
(or is the expected output 15, after just a single pass through the digits?)
If you want only a single pass through the digits:
#include <stdlib.h>
#include <stdio.h>
int main()
{
int i = 12345;
int sum=0;
i = abs(i);
while(i)
{
sum += i%10;
i /= 10;
}
printf ("Sum of digits is %d\n", sum);
return 0;
}

How to find the missing number without using arrays?

I have to find a missing number in a sequence of numbers.
The input consists of a positive integer n, between 0 and 35000, and n unique numbers with range [0..n]. (So this range contains n+1 numbers).
I already tried some things with sum={n*(n+1)}/2 and then misNum=sum-SumOfNum;, but I couldn't find a way to make this work.
I wrote some code, but not with the examples I mentioned before. Obviously, this code is not complete, but I don't know how to make it complete.
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *v[]) {
int length;
int num;
scanf("%d", &length);
/*scanf(???)*/
int goal=length;
int i;
for(i=0; i!=length; i++){
goal=goal+i-num[i];
};
return goal;
}
Input and outcome should be:
Input: 2 "enter" 0 2. Output: 1
Input: 3 "enter" 0 3 1. Output: 2
Sum of all numbers from 0 to n is
n(a1+an)/2 = (in your case a1 = 0 and an = n+1) n*(n+1)/2
so the missing number is n*(n+1)/2 - (sum of input numbers after the length)
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* v[]) {
int length;
int i = 0;
int sum = 0;
scanf_s("%d", &length);
// calculate arithmetic series sum
auto series_sum = ((length + 1) * (length)) / 2;
while (i < length)
{
int next;
scanf_s("%d", &next);
sum += next;
++i;
}
printf("missing num is %d ", series_sum - sum);
}
You have n number of integers to be scanned. Use mathematical equation to calculate the sum of first n+1 natural numbers. Then run a loop for n times and then run a loop to add all the n numbers scanned. Then subtract this sum with the sum of n+1 natural number. Result will be the missing number.
The calculation from the question is also correct and can be made to work with a few modifications.
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *v[]) {
int length;
int num;
// printf("enter maximum number: ");
scanf("%d", &length);
int goal=length;
int i;
for(i=0; i!=length; i++){
// printf("number[%d]: ", i);
if(scanf("%d", &num) != 1) {
fprintf(stderr, "invalid input\n");
return 1;
}
if((num < 0) || (num > length)) {
fprintf(stderr, "invalid number %d\n", num);
return 2;
}
goal=goal+i-num;
};
// printf("missing number: ");
printf("%d\n", goal);
return 0;
}

How to invent a code for square-root?

I want to write a code for making square-root not using pow().
here is what i have tried:
#include <stdio.h>
#include <stdlib.h>
int main(){
int a,I,sum=0,cnt=0;
printf("enter number");
scanf("%d",&a);
for(I=1;sum<a;I+=2){
sum+=I;
cnt++;
}
printf("answer is:%d",cnt);
return 0;
}
for numbers like 4,9,16,... it works but for numbers like 10,17,21,.. it does not work and the result is more than it shoud be.
what is the problem?
for(I=1;;I+=2){
sum+=I;
if(sum>a)
break;
cnt++;
}
#include <stdio.h>
#include <stdlib.h>
int main(){
int a,I,sum=0,cnt=0;
printf("enter number");
scanf("%d",&a);
for(I=1;sum<a;I+=2){
sum+=I;
cnt++;
}
if(sum==a)
printf("answer is:%d",cnt);
else
printf("answer is:%d",cnt-1);
return 0;
}
You can try this
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,I,cnt=0;
int sum = 0;
printf("enter number");
scanf("%d",&a);
for(I=1;sum<a;I+=2){
sum+=I;
cnt++;
if(sum > a) //add this if statement to decrement cnt by 1 when sum exceeds a.
cnt--;
}
printf("answer is:%d",cnt);
}
Input:
21
Output:
4
Use the Babylonian method:
double babyl_sqrt(double x)
{
double i;
for (i = x / 2; fabs(i * i - x) > 0.000001f; i = (i + x / i) / 2)
;
return i;
}
To get a rounded to nearest int, adjust the limit a little bit.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int sqrt_round(int a) {
int I;
int sum = 0;
int cnt = 0;
// for(I=1;sum<a;I+=2){
for (I = 1; (sum + I / 2) < a; I += 2) {
sum += I;
cnt++;
}
return cnt;
}
int main() {
int a;
printf("enter number");
scanf("%d", &a);
printf("answer is:%d\n", sqrt_round(a));
printf("answer is:%g\n", sqrt(a));
return 0;
}
#include<stdio.h>
int main()
{
float i,x=10;
int lp;
scanf("%f",&i);
for(lp=0;lp<5;lp++)
x=(x-((((x*x)-i))/(2*x)));
printf("sqaure root of %f=%f\n",i,x);
return 0;
}
Compute and return the square root of x, where x is guaranteed to be a non-negative integer.
Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned.
Example 1:
Input: 4
Output: 2
Example 2:
Input: 8
Output: 2
Explanation: The square root of 8 is 2.82842..., and since
the decimal part is truncated, 2 is returned.
public int mySqrt(int x) {
long start = 1;
long end = x;
while(start + 1< end) {
long mid = start + (end - start) / 2 ;
if(mid * mid == x) {
return (int)mid;
}else if(mid * mid < x) {
start = mid ;
}else {
end = mid;
}
}
if(end * end == x) {
return (int)end;
}
return (int)start;
}

Resources