I need help with these two problems. For the second problem, I posted the code b
ID: 3796624 • Letter: I
Question
I need help with these two problems. For the second problem, I posted the code below that is mentioned to be in "Lab 4", however, it needs to be adjusted.
Language in C...not C++.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void) {
int n = 1000;
//seed the random number generator
srand(time(NULL));
int number = (rand() % n) + 1; // secret number
int guess = -10;
int num_guesses = 0;
printf("Guess-A-Number Game! ");
printf("Enter a number between 1 and %d ", n);
scanf("%d", &guess);
num_guesses = num_guesses + 1; // alternative: num_guesses++
//TODO: place your code here
while(guess != number) {
if(guess > number) {
printf("%d is too high ", guess);
}
else {
printf("%d is too low ", guess);
}
// Repeat by asking for another guess
printf("Enter another guess: ");
scanf("%d", &guess);
num_guesses = num_guesses + 1;
}
printf("Congratulations, you found it! Number of guesses: %d ", num_guesses);
return 0;
}
Explanation / Answer
2)program:-
#include<stdio.h>
void main()
{
int number;
printf("Enter Number of terms :");
scanf("%d",&number);
printf(" the sequence is : 0 1 ");
fact(number);
}
void fact(int n)
{
int i;
int f1=0,f2=1,f3;
for(i=2;i<n;i++)
{
f3=f2+f1;
printf("%d ",f3);
f1=f2;
f2=f3;
}
}
Output:-
i)
Enter Number of terms :5
the sequence is : 0 1 1 2 3
Process exited with return value 5
Press any key to continue . . .
ii)
Enter Number of terms :15
the sequence is : 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Process exited with return value 15
Press any key to continue . . .
3) program:-
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int num_guesses=0;
int guess = -10;
int main(void)
{
int n = 10,t;
//seed the random number generator
srand(time(NULL));
int number = (rand() % n) + 1; // secret number
printf("Guess-A-Number Game! ");
printf("Enter a number between 1 and %d ", n);
scanf("%d", &guess);
num_guesses = num_guesses + 1; // alternative: num_guesses++
t=check(guess,number);
if(t>0&&t<11)
printf("Congratulations, you found it! Number of guesses: %d ", num_guesses);
else
printf("You did not found it! Number of guesses: %d ", num_guesses);
return 0;
}
int check(int n,int number)
{
//TODO: place your code here
for(num_guesses=1;num_guesses<=10&&guess!=number;num_guesses++) //for loop placed here instead of while
{
if(guess>0&&guess<11)
{
if(guess > number)
{
printf("%d is too high ", guess);
}
else
{
printf("%d is too low ", guess);
}
// Repeat by asking for another guess
printf("Enter another guess: ");
scanf("%d", &guess);
}
else
{
num_guesses--;
scanf("%d", &guess);
}
}
return num_guesses;
}
Output:-
i)
Guess-A-Number Game!
Enter a number between 1 and 10
5
5 is too low
Enter another guess: 22
5
5 is too low
Enter another guess: 41
5
5 is too low
Enter another guess: 8
8 is too low
Enter another guess: 10
Congratulations, you found it! Number of guesses: 5
Process exited normally.
Press any key to continue . . .
ii)
Guess-A-Number Game!
Enter a number between 1 and 10
1
1 is too low
Enter another guess: 2
2 is too low
Enter another guess: 10
10 is too high
Enter another guess: 10
10 is too high
Enter another guess: 2
2 is too low
Enter another guess: 10
10 is too high
Enter another guess: 1
1 is too low
Enter another guess: 1
1 is too low
Enter another guess: 10
10 is too high
Enter another guess: 2
2 is too low
Enter another guess: 1
You did not found it! Number of guesses: 11
Process exited normally.
Press any key to continue . . .