Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

chaguanas Group Title

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.

  • one year ago
  • one year ago

  • This Question is Open
  1. chaguanas Group Title
    Best Response
    You've already chosen the best response.
    Medals 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; }

    • one year ago
  2. KonradZuse Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Whuat exactly is the issue?

    • one year ago
  3. chaguanas Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    I 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.

    • one year ago
  4. chaguanas Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    I'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.

    • one year ago
  5. KonradZuse Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Basically 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.

    • one year ago
  6. chaguanas Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Thank 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.

    • one year ago
  7. KonradZuse Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    yup anything else needed?

    • one year ago
  8. chaguanas Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    OK, here is another code, my classmate started, it is not finished, you might get the idea; the assignment was to write a menu-driven 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.

    • one year ago
  9. chaguanas Group Title
    Best Response
    You've already chosen the best response.
    Medals 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)); }

    • one year ago
  10. KonradZuse Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

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

    • one year ago
  11. chaguanas Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    OK, 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.

    • one year ago
  12. madhav12 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    http://myopencourses.com/subject/coding-theory - see if this helps

    • one year ago
    • Attachments:

See more questions >>>

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
  • 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.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.