thanks a lot for opening the question. can anyone find out the logic error for m
ID: 3857547 • Letter: T
Question
thanks a lot for opening the question.
can anyone find out the logic error for me?
this is in C
#include<stdio.h>
#include<math.h>
#include<stdbool.h>
#include<ctype.h>
#include<stdlib.h>
void flushStdin();
int getmid(int low,int high);
int main(){
int usernum=0;
char userChar=' ';
int low=0;
int high=0;
int mid=0;
int lastGuess=0;
bool iamright=false;
bool absurd=false;
bool isvalidchar=true;
int i=1;
printf("Enter n: ");
scanf("%d",&usernum);
if((scanf("%d",&usernum))!=1)
{
printf("Error: invalid input. ");
return 0;
}
if(usernum<=0) {
printf("Error: %d must be positive. ",usernum);
return 0;
}
else{
high=usernum;
low=getmid(low,high);
printf("Is your number %d? ",low);
lastGuess=low;
for(i=1;!iamright&&!absurd&&isvalidchar;){//forloop
if(userChar!='l'&&userChar!='s'&&userChar!='e'){
isvalidchar=false;
}
else {userChar=getchar();
flushStdin();
userChar=tolower(userChar);
switch(userChar){
case'l':
if((high-1)==low){
absurd=true;
}
else{low=lastGuess;
mid=getmid(low,high);
lastGuess=mid;
printf("Is your number %d?",mid);
++i;
break;
}
case 's':
if((low+1)==high){
absurd=true;
}
else{high=lastGuess;
mid=(getmid(low,high));
lastGuess=mid;
printf("Is your number %d?",mid);
++i;}
break;
case 'e':
iamright=true;
break;
}
}
}
}
if(iamright){
printf("Your number must be %d. I used %d guesses.",mid,i);
}
if(absurd){
printf("Error: that’s not possible. ");
}
if(!isvalidchar){
printf("Error: invalid input. ");
}
return 0;
}
void flushStdin(){
char c;
while ((c=getchar())!=EOF&&c!=' ')
{
}
}
int getmid(int low,int high){
return floor((double)(low+high)/2);
}
Explanation / Answer
#include<stdio.h>
#include<math.h>
#include<stdbool.h>
#include<ctype.h>
#include<stdlib.h>
void flushStdin();
int getmid(int low,int high);
int main(){
int usernum=0;
char userChar=' ';
int low=0;
int high=0;
int mid=0;
int lastGuess=0;
bool iamright=false;
bool absurd=false;
bool isvalidchar=true;
int i=1;
printf("Enter n: ");
scanf("%d",&usernum);
if((scanf("%d",&usernum))!=1)
{
printf("Error: invalid input. ");
return 0;
}
if(usernum<=0) {
printf("Error: %d must be positive. ",usernum);
return 0;
}
else{
high=usernum;
low=getmid(low,high);
printf("Is your number %d? ",low);
lastGuess=low;
for(i=1;!iamright&&!absurd&&isvalidchar;){//forloop
if(userChar!='l'&&userChar!='s'&&userChar!='e'){
isvalidchar=false;
}
else {userChar=getchar();
flushStdin();
userChar=tolower(userChar);
switch(userChar){
case'l':
if((high-1)==low){
absurd=true;
}
else{low=lastGuess;
mid=getmid(low,high);
lastGuess=mid;
printf("Is your number %d?",mid);
++i;
break;
}
case 's':
if((low+1)==high){
absurd=true;
}
else{high=lastGuess;
mid=(getmid(low,high));
lastGuess=mid;
printf("Is your number %d?",mid);
++i;}
break;
case 'e':
iamright=true;
break;
}
}
}
}
if(iamright){
printf("Your number must be %d. I used %d guesses.",mid,i);
}
if(absurd){
printf("Error: that’s not possible. ");
}
if(!isvalidchar){
printf("Error: invalid input. ");
}
return 0;
}
void flushStdin(){
char c;
while ((c=getchar())!=EOF&&c!=' ')
{
}
}
int getmid(int low,int high){
return floor((double)(low+high)/2);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.