A community for students.
Here's the question you clicked on:
 0 viewing
chaguanas
 3 years ago
Please explain this C code (follows in text). It is code for a guessing game (guess the right number). I am a beginning student and this example comes from my textbook, but I can understand the while loop, if, else, the call to functions; also it uses words as variables and I can't help interpreting the meaning, where as a computer just sees it as a variable or string.
chaguanas
 3 years ago
Please explain this C code (follows in text). It is code for a guessing game (guess the right number). I am a beginning student and this example comes from my textbook, but I can understand the while loop, if, else, the call to functions; also it uses words as variables and I can't help interpreting the meaning, where as a computer just sees it as a variable or string.

This Question is Open

chaguanas
 3 years ago
Best ResponseYou've already chosen the best response.0#include <stdio.h> #define TARGET 17 #define LOW 0 #define HIGH 100 #define TRUE 1 #define FALSE 0 int process( int guess, int target ); int. main (void) { Int a_guess, correct; correct = FALSE; printf( “I’m thinking of a number between %d and %d.\n”, LOW, HIGH ); while ( correct == FALSE ) { printf( “Try to guess it now. “ ); scanf( “%d”, &a_guess ); correct = process( a_guess, TARGET ); } return 0; int process( int guess, int target ) { If ( guess < target ) printf( “Too low!\n” ) else if ( guess > target ) printf( “Too high!\n” ); else { printf( “You guessed it!\n” )’ return TRUE; } Return FALSE; }

KonradZuse
 3 years ago
Best ResponseYou've already chosen the best response.0Whuat exactly is the issue?

chaguanas
 3 years ago
Best ResponseYou've already chosen the best response.0I don't understand the nuts and the bolts. Like how the while function works, how correct is assigned FALSE, then correct is assigned process. In class, we are just breezing through everything, and I seem stuck because I can't comprehend it.

chaguanas
 3 years ago
Best ResponseYou've already chosen the best response.0I'm kind of piecing it together, the more I look at it, the 'process' function eventually returns FALSE, which tells the while loop to terminate.

KonradZuse
 3 years ago
Best ResponseYou've already chosen the best response.0Basically while works that it will keep asking the question until your "target" guess is correct. Low is 0, high is 100. The target is 17. So basically you are asked a # between 0 and 100. While it's false, it will ask you to enter a number, if you enter ANY number besides 17 you will goto the if statements stating it if is too high, or too low. When you enter 17 it will tell you you are correct, and will set it to true, so the while loop stops.

chaguanas
 3 years ago
Best ResponseYou've already chosen the best response.0Thank you, my text book is kind of verbose and detailed and speaks the 'C language'. It never gives a 'English' explanation as you gave above. So I'm having trouble with basics here and there, but I'm getting it.

KonradZuse
 3 years ago
Best ResponseYou've already chosen the best response.0yup anything else needed?

chaguanas
 3 years ago
Best ResponseYou've already chosen the best response.0OK, here is another code, my classmate started, it is not finished, you might get the idea; the assignment was to write a menudriven mini statistics package. I guess I get a little intimidated by all the code, but I don't follow a lot of this, if you can tell me what is going on.

chaguanas
 3 years ago
Best ResponseYou've already chosen the best response.0#include <math.h> #include "stdafx.h" #include <stdio.h> #define MAX 200 void print_inventory( int inventory[], int numbitems); int stdDev( int inventory[], int num_items, int meen) int input_inventory( int inventory[], int maximum); int largest( int inventory[], int n); int mean(int inventory[],int n); /******************************************main***************************/ int main( void ) { int inventory[MAX]; int num_item; int large; int meen; int dev; printf( "enter 1  200 differnt numbers\n"); num_item = input_inventory( inventory, MAX); print_inventory( inventory, num_item ); large = largest( inventory, num_item); printf( "%d is the largest number\n",inventory[large]); meen = mean( inventory, num_item ); printf(" mean of array is %d\n", meen); dev = stdDev( inventory, num_item, meen); printf(" the standard deviation is %d",dev); return 0; } /***************************input items***************************/ int input_inventory( int inventory[], int maximum) { int index = 0; scanf_s( "%d", &inventory[ index] ); while ( index < maximum && inventory[ index ] != 1) { index++; scanf_s( "%d", &inventory[index] ); } if (index == 1) { printf( "No room for more items."); return( index+1); } else return ( index); } /*************************print items******************/ void print_inventory( int inventory[], int numitems) { int index; for( index= 0; index < numitems; index++) { printf(" item number %d;\t", index +1); printf( "number on hand %5d\n",inventory[index]); } } /****************************mean*******************/ int mean(int inventory[],int n) { int i; int sum=0; for(i=0;i<n;i++) sum=sum+inventory[i]; return (sum/n); } /*****************largest********************/ int largest( int inventory[], int n) { int i, index = 0; for( i = 0; i <= n; i++) if( inventory[index] < inventory[i] ) index = i; return index; } /****************************diveation***********************/ int stdDev( int inventory[], int num_items, int meen) { int i; int sumdevs = 0; for( i = 0; 9 < num_items; i++) sumdevs = sumdevs + (inventory[i]  meen)^2; return((sumdevs/num_items)^(1/2)); }

KonradZuse
 3 years ago
Best ResponseYou've already chosen the best response.0he's creating a bunch of methods, and each method does something different. In coding making certain bits of code is important. Lets say you wan tto add something, so you could make a method like int add(int a, int b) { return a + b; }

chaguanas
 3 years ago
Best ResponseYou've already chosen the best response.0OK, thank you. I think I'm going to stop looking at the vast code and being intimidated, I eat it in little nugget size like your example.

madhav12
 3 years ago
Best ResponseYou've already chosen the best response.0http://myopencourses.com/subject/codingtheory  see if this helps
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.