Thread: Homework Help! Please!

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    1

    Homework Help! Please!

    Im a beginner and i need help on an assignment due at midnight.

    1) Find workers pay if rate is $9.00 and they get overtime (1.5x) after 40 hours

    Code:
    #include <stdio.h>
    
    
    int main(){
      int hours;
      double pay;
      
      printf("Enter the number of hours: \n");
      scanf("%d", &hours);
      
      if(hours>40){
      pay = hours*9.00*1.5;
      }
      else if(hours<=40){
      pay = hours*9.00;
      }
      printf("Your pay is %.2f", &pay);
      
      return 0;
      
    }
    This first one returns $0.00 no matter how many hours you put in.



    2) Write program that will transcript message based on GPA

    0-0.99 Failed
    1-1.99 Suspended
    2-2.99 no message
    3-3.5 dean's list
    4-4.0 highest scholar award

    Code:
    #include <stdio.h>
    
    
    int main(){
      
      double GPA;
      
      printf("Enter your GPA: \n");
      scanf("%d", GPA);
      
      if(GPA <= 4.0 && GPA >= 3.5)
        printf("Highest honors for semester\n");
      else if(GPA < 3.5 && GPA >= 3.0)
        printf("Dean's list for semester\n");
      else if(GPA < 3.0 && GPA >= 2.0)
        printf("\n");
      else if(GPA < 2.0 && GPA >= 1.0)
        printf("On probation for next semester\n");
      else if(GPA < 1.0)
        printf("Failed semester - Registration suspended\n");
        
        return 0;
    }
    This one always returns the last line even if you put in a 4.0 GPA for example

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    1) You pass the address of 'pay' and not the value it expects.
    2) You pass the value of 'GPA' and not the address it expects.

    As you see, you've got the usages of printf and scanf backwards.
    Devoted my life to programming...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Also, %d is not how you printf or scan double values.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Code:
    #include <stdio.h>
    
    int main (void)
    {
      double gpa;
       
      printf("Enter your GPA: \n");
      scanf("%lf", &gpa);
     
        if ( gpa <= 1.0 )
            printf("study and pratice more\n");
        else if (gpa > 1.0 && gpa <= 2.0 )
            printf ("you're getting better\n");
        else
            printf ("remarkable scores !\n");
            
    return 0;
    }
    process of elimination, and you forgot your & <-- in your scanf and like said, d% is for int not float or double. As far as you logic flow. that is up to you, I just thought you might like to see a different way of doing it.

    as far as this.
    Code:
    2-2.99 no message
    try just skipping that range when your checking.

    printf - C++ Reference
    Last edited by userxbw; 10-10-2017 at 08:59 AM.

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    again, process of elimination.
    Code:
     if(hours>40)
        pay = (hours*9.00)*1.5;
      else 
        pay = hours*9.00;
    and check your printf on your output for amount paid out. is that really formatted properly?

    if...else statement in C
    Last edited by userxbw; 10-10-2017 at 09:21 AM.

  6. #6
    Registered User
    Join Date
    Oct 2014
    Posts
    33
    1. Instead of "using else if" you can use just "else" for the last condition. Keeps the program simpler.
    2. There is no need to declare the variables as double in your case. Using float and scanf it with %f would suffice.
    3.When printing you don't use &. YOu don't need to print the address.
    Just do it as printf("pay is", pay);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By Abbie Fortner in forum C Programming
    Replies: 6
    Last Post: 02-25-2016, 06:59 PM
  2. Please help me with my c++ homework!
    By dmeyers81 in forum C++ Programming
    Replies: 35
    Last Post: 11-04-2010, 07:47 AM
  3. Homework
    By cagurtay in forum C Programming
    Replies: 3
    Last Post: 11-01-2010, 05:28 PM
  4. Homework Help
    By shadowctr in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2005, 07:29 PM
  5. Homework Help Please
    By mars4bb in forum C++ Programming
    Replies: 3
    Last Post: 09-26-2005, 05:16 PM

Tags for this Thread