Need Help with Homework.

This is a discussion on Need Help with Homework. within the C Programming forums, part of the General Programming Boards category; I have an assignment due in Programming 1 and I need a little bit of help. I have already created ...

  1. #1
    Registered User skaldicpoet9's Avatar
    Join Date
    Dec 2007
    Posts
    15

    Need Help with Homework.

    I have an assignment due in Programming 1 and I need a little bit of help. I have already created the program but now I have one last thing to do. However, I don't quite understand what my professor wants me to do. Here are the instructions she gave me:

    "Use a single subprogram to prompt and read user's input for the numbers to be used in the calculations.
    Note: It must be used to get all input for calculations."


    I am not sure what that means.

    Here is the code:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    /*
      Stephen Haroldson
      11:00am
    */
    
    int menu(void);
    
    int main(void) {
       int selection,num1,num2,ans,fans;
       selection = menu();
       while(selection != 8)  {
          switch(selection) {
              case 1: printf("Enter 1st number: ");
                      scanf("&#37;d", &num1);
                      printf("Enter 2nd number: ");
                      scanf("%d", &num2);
                      ans = num1 + num2;
                      printf("%d + %d = %d\n",num1,num2,ans);
                      break;
              case 2: printf("Enter 1st number: ");
                      scanf("%d", &num1);
                      printf("Enter 2nd number: ");
                      scanf("%d", &num2);
                      ans = num1 - num2;
                      printf("%d - %d = %d\n",num1,num2,ans);
                      break;
              case 3: printf("Enter 1st number: ");
                      scanf("%d", &num1);
                      printf("Enter 2nd number: ");
                      scanf("%d", &num2);
                      ans = num1 * num2;
                      printf("%d * %d = %d\n",num1,num2,ans);
                      break;
              case 4: printf("Enter 1st number: ");
                      scanf("%d", &num1);
                      printf("Enter 2nd number: ");
                      scanf("%d", &num2);
                      if(num2 == 0) {
                      printf("Zero is not an acceptable value!\n");
                      break; }
                      fans =(float)num1/num2;
                      printf("%d / %d = %2d\n",num1,num2,fans);
                      break;
              case 5: printf("Enter 1st number: ");
                      scanf("%d", &num1);
                      ans = abs(num1);
                      printf("|%d| = |%d|\n",num1,ans);
                      break;
              case 6: printf("Enter 1st number: ");
                      scanf("%d",&num1);
                      if(num1 < 0){
                      printf("You cannot enter in a negative value!\n");
                      break; }
                      fans = sqrt(num1);
                      printf("sqrt(%d) = %d\n",num1,fans);
                      break;
              case 7: printf("Enter base: ");
                      scanf("%d", &num1);
                      if((num1 == 0)||(num2 <= 0)) {
                      printf("That is not an acceptable value!\n");
                      break; }
                      printf("Enter exponent: ");
                      scanf("%d", &num2);
                      fans = pow(num1, num2);
                      printf("%d ^ %d = %d\n",num1,num2,fans);
                      break;
             default: printf("%d is not a valid selection\n", selection);
           }
           selection = menu();
        }
        printf("Bye!\n");
      }
    
    
       int menu(void) {
          int choice;
          printf("1 Add\n");
          printf("2 Subtract\n");
          printf("3 Multiply\n");
          printf("4 Divide\n");
          printf("5 Absolute Value\n");
          printf("6 Square Root\n");
          printf("7 Powers\n");
          printf("8 Quit\n");
          printf("Enter a Selection: ");
          scanf("%d", &choice);
          return choice;
       }
    If anyone can help me I would appreciate it a lot
    Last edited by skaldicpoet9; 02-07-2008 at 09:02 PM.

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    What he means there is, you should have function where that is responsible in reading all values from the user any calculation you do in the main. So for example, you have so man scanf in your main function. Create a function and have just one scanf function there and return the read value back to main.

    ssharish

  3. #3
    Registered User skaldicpoet9's Avatar
    Join Date
    Dec 2007
    Posts
    15
    So I would have to create an if statement for every form of math that the user inputs?
    Something like:

    Code:
    if(symbol = +) {
                scanf("%d", "%d", "%d",num1,symbol,num2);
                ans = num1 + num2;
                printf("%d + %d = %d\n",num1,num2,ans);  
    }
    As far as I understand what your saying...

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by skaldicpoet9 View Post
    So I would have to create an if statement for every form of math that the user inputs?
    Something like:

    Code:
    if(symbol = +) {
                scanf("%d", "%d", "%d",num1,symbol,num2);
                ans = num1 + num2;
                printf("%d + %d = %d\n",num1,num2,ans);  
    }
    As far as I understand what your saying...
    Not really, no. Look at your original code: more specifically, look at your inputting-of-numbers. Do you see any patterns there? Maybe that repeated code could be made into a routine -- C uses functions for routines as well, so it's going to be a function. So write a function that inputs two numbers. (Note that you can't return two things from a function -- you'll have to use pass-by-pointer with the two variables you want read.)

  5. #5
    Registered User skaldicpoet9's Avatar
    Join Date
    Dec 2007
    Posts
    15
    Ok, I think I understand what you are saying but what is a pass-by-pointer?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    How familiar are you with functions? If you have a function with prototype
    Code:
    void my_function(int I_am_a_number, int *I_am_a_pointer);
    then the first parameter is passed by value, which means a copy is made and the function deals with the copy. The second parameter is passed by pointer (a/k/a passed by reference) which means you are able to directly access the (same) memory in your function.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by skaldicpoet9 View Post
    Ok, I think I understand what you are saying but what is a pass-by-pointer?
    Code:
    #include <stdio.h>
    void readval( int *val )
    {
         scanf("%d", val);
    }
    int main()
    {
        int num;
        
        /* this is pass by pointer */
        readval( &num );
        
        printf("%d\n", num );
        
        return 0;
    }
    Look at the sample code above. More specifically look at the whu send the num as a parameter to functon readval. I am sending the address of num to readval. That is what is called pass by pointer.

    ssharish

  8. #8
    Registered User skaldicpoet9's Avatar
    Join Date
    Dec 2007
    Posts
    15
    Ok, I understand now. Thanks for all of your help

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Code:
              case 4: printf("Enter 1st number: ");
                      scanf("%d", &num1);
                      printf("Enter 2nd number: ");
                      scanf("%d", &num2);
                      if(num2 == 0) {
                      printf("Zero is not an acceptable value!\n");
                      break; }
                      fans =(float)num1/num2;
                      printf("%d / %d = %2d\n",num1,num2,fans);
                      break;
    I must stress to you that writing code that way is very bad. Hard to read, easy to miss. Creates bugs. Every block or if should stand out, therefore I recommend you write it this way:
    Code:
              case 4: printf("Enter 1st number: ");
                      scanf("%d", &num1);
                      printf("Enter 2nd number: ");
                      scanf("%d", &num2);
                      if(num2 == 0) {
                          printf("Zero is not an acceptable value!\n");
                          break;
                      }
                      fans =(float)num1/num2;
                      printf("%d / %d = %2d\n",num1,num2,fans);
                      break;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 03:39 PM
  2. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 03:16 PM
  3. Homework
    By kermi3 in forum Windows Programming
    Replies: 5
    Last Post: 09-15-2001, 11:48 AM
  4. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21