Thread: Help in HomeWork

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    12

    Help in HomeWork

    in this homework i try to use do while and function in the beginning but doesn't work so i decided to use switch and its dose not work too i dont know why .. he just always go directly to the default


    Code:
    #include <stdio.h> 
    
    
    int main ()
    {
        int times = 1 , current , voltage,type ;
        double power , cost;
    do
    {
         printf("Recipient #%d information:\n",times);
         printf("\nInsert current > ");
         scanf("%d",&current);
         printf("\nInsert voltage > ");
         scanf ("%d",&voltage);
         printf("\nEnter Consumption Category:\n");
         printf("1 = Residential\n");
         printf("2 = Commercial\n");
         printf("3 = Agricultural & charities\n");
         printf("4 = Government \n");
         printf("5 = Industrial \n");
         printf("6 = Private Educational and Medical\n");
         scanf("%d",&type);
         switch (type)
        {
            case 1 : 
                power = current*voltage/1000;
         printf("Power =%.2lf kilo watts",power);
        if (1<power<6000)
            {cost = 6000 * 1.8;
            printf("The cost = %.2lf",cost);}
            else 
            {cost =6000 * 1.8+(1000*0.3);}
            printf("The cost = %.2lf",cost);
            break ;    
            case 2 :
                power = current*voltage/1000;
         printf("Power =%.2lf kilo watts",power);
        if (1<power<6000)
            {cost = 6000*0.2;
            printf("The cost = %.2lf",cost);}
            else
            {cost = 6000*0.2+(1000*0.3);    
            printf("The cost = %.2lf",cost);}
            break ;
            case 3 :
                power = current*voltage/1000;
         printf("Power =%.2lf kilo watts",power);
        if (1<power<6000)
         {cost = 6000*0.16;
         printf("The cost = %.2lf",cost);}
            else
        {cost = 6000*0.16+(1000*0.2);
        printf("The cost = %.2lf",cost);}
            break ;
            case 4 :
                power = current*voltage/1000;
         printf("Power =%.2lf kilo watts",power);
        if (1<power<6000)
        {cost = 6000*0.32;
        printf("The cost = %.2lf",cost);}
            else
        {cost = 6000*0.32+(1000*0.32);
        printf("The cost = %.2lf",cost);}    
            break ;
            case 5:
                power = current*voltage/1000;
         printf("Power =%.2lf kilo watts",power);
            if (1<power<6000)
        {cost = 6000*0.18;
        printf("The cost = %.2lf",cost);}
            else
        {cost = 6000*0.18+(1000*0.18);
        printf("The cost = %.2lf",cost);}
            break;
            case 6 :
                power = current*voltage/1000;
         printf("Power =%.2lf kilo watts",power);
        if (1<power<6000)
        {cost = 6000*0.18;
        printf("The cost = %.2lf",cost);}
            else
        {cost = 6000*0.18+(1000*0.18);
        printf("The cost = %.2lf",cost);}
            break;
        default : printf ("Incorrect Catrgory ! program will stop now ! ");    
           break;
        }
        times+=1;
    }
    while (current !=-1 && voltage!=-1);
    printf("No recipient Information Is inserted ");
    return 0;
    
    
    }
    and this the document of the homework ...



    Adobe Document Cloud


    And this is how the output looks

    Help in HomeWork-outnooob-png
    Last edited by mishal07; 10-25-2018 at 03:19 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    %lf cannot be used to input into an integer. %d can, though.

  3. #3
    Registered User
    Join Date
    Oct 2018
    Posts
    12
    will thx its fixed but i have more 2 problems

    the first the while condition not work

    Attachment 15533


    and the other is i dont know i can make the max and min and the total for both
    like what is show here

    Attachment 15534

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Whatever you tried to attach doesn't seem to have attached.

  5. #5
    Registered User
    Join Date
    Oct 2018
    Posts
    12
    will thx its fixed but i have more 2 problems

    the first the while condition not work
    Help in HomeWork-p_10298igw61-jpg
    and the other is i dont know i can make the max and min and the total for both
    like what is show here
    Help in HomeWork-p_10299dq712-png

    its seems like if i enter -1 the program will stop and count the total and the max and the min
    so i need to know how i can do this in easy way

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if (1<power<6000)
    This isn't how you compare a value is between two range limits.

    Your indentation needs work (as per the guidelines in your assignment).

    You really should start to use functions to stop main becoming a ball of mud.

    For example,
    Code:
    // add parameters / return result(s) as required.
    void processConsumer() {
         printf("\nEnter Consumption Category:\n");
         printf("1 = Residential\n");
         printf("2 = Commercial\n");
         printf("3 = Agricultural & charities\n");
         printf("4 = Government \n");
         printf("5 = Industrial \n");
         printf("6 = Private Educational and Medical\n");
         scanf("%d",&type);
         // more of your code here
    }
    
    int main ( ) {
         // more of your code here
        printf("Recipient #%d information:\n",times);
        printf("\nInsert current > ");
        scanf("%d",&current);
        printf("\nInsert voltage > ");
        scanf ("%d",&voltage);
        if ( voltage != -1 && current != -1 ) {
            processConsumer();
        } else {
            // print the final stats
        }
         // more of your code here
    }
    You store the max using the following code
    Code:
    int max = -1; // better is -INT_MAX
    
    /// more code
    
    // Keep track of the current max
    if ( value > max ) max = value;
    Minimum works in the same way, except you start with an impossibly large value, and you use < rather than >.
    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.

  7. #7
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    138
    Quote Originally Posted by Salem View Post

    [...]

    Code:
    int max = -1; // better is -INT_MAX
    
    /// more code
    
    // Keep track of the current max
    if ( value > max ) max = value;
    [...]

    .
    Is not INT_MIN better than -INT_MAX ?

  8. #8
    Registered User
    Join Date
    Oct 2018
    Posts
    12
    Quote Originally Posted by Salem View Post
    > if (1<power<6000)
    This isn't how you compare a value is between two range limits.

    Your indentation needs work (as per the guidelines in your assignment).

    You really should start to use functions to stop main becoming a ball of mud.

    For example,
    Code:
    // add parameters / return result(s) as required.
    void processConsumer() {
         printf("\nEnter Consumption Category:\n");
         printf("1 = Residential\n");
         printf("2 = Commercial\n");
         printf("3 = Agricultural & charities\n");
         printf("4 = Government \n");
         printf("5 = Industrial \n");
         printf("6 = Private Educational and Medical\n");
         scanf("%d",&type);
         // more of your code here
    }
    
    int main ( ) {
         // more of your code here
        printf("Recipient #%d information:\n",times);
        printf("\nInsert current > ");
        scanf("%d",&current);
        printf("\nInsert voltage > ");
        scanf ("%d",&voltage);
        if ( voltage != -1 && current != -1 ) {
            processConsumer();
        } else {
            // print the final stats
        }
         // more of your code here
    }
    You store the max using the following code
    Code:
    int max = -1; // better is -INT_MAX
    
    /// more code
    
    // Keep track of the current max
    if ( value > max ) max = value;
    Minimum works in the same way, except you start with an impossibly large value, and you use < rather than >.






    THE problem is i have to use the switch here and the function make this more difficult
    if you open the file of the homework you will note that he ask me to use the table
    so each number in the menu have a special number or special condition

  9. #9
    Registered User
    Join Date
    Oct 2018
    Posts
    12
    this what i mange the last hours i fix everything except the while condition be like a hole of miss
    Help in HomeWork-final-dex-png

    and the other problem i do not understand you when you say i can do the min in the same why but i need to put the largest value ?



    Code:
    #include <stdio.h> 
    
    int main ()
    {
    	int times = 1 ,type ;
    	double power , cost,TotalPower,TotalCost,max=0,min=0 ,current , voltage;
    do
    {
    	 printf("\nRecipient #%d information:\n",times);
    	 printf("\nInsert current > ");
    	 scanf("%lf",&current);
    	 printf("\nInsert voltage > ");
    	 scanf ("%lf",&voltage);
    	 if (times==1&&current==-1&&voltage==-1)
    	 {printf("no recipient information is inserted ");
    	 return 0;}
    	 printf("\nEnter Consumption Category:\n");
    	 printf("1 = Residential\n");
    	 printf("2 = Commercial\n");
    	 printf("3 = Agricultural & charities\n");
    	 printf("4 = Government \n");
    	 printf("5 = Industrial \n");
    	 printf("6 = Private Educational and Medical\n");
    	 scanf("%d",&type);
    	 switch (type)
    	{
    		case 1 : 
    			power = current*voltage/1000;
    	 printf("Power =%.2lf kilo watts\n",power);
    	if (power>6000)
    	{cost =(6000 * 0.18)+((power-6000)*(0.3));
    		printf("The cost = %.2lf Rs\n",cost);}
    		else 
    		{cost = 6000 * 1.8;
    		printf("The cost = %.2lf RS\n",cost);}
    		break ;	
    		case 2 :
    			power = current*voltage/1000;
    	 printf("Power =%.2lf kilo watts\n",power);
    	if (power>6000)
    	    {cost = (6000*0.20)+((power-6000)*(0.30));	
    		printf("The cost = %.2lf RS\n",cost);}
    		else
    		{cost = 6000*0.2;
    	    printf("The cost = %.2lf RS\n",cost);}
    		break ;
    		case 3 :
    			power = current*voltage/1000;
    	 printf("Power =%.2lf kilo watts",power);
    	if (power>6000)
    	 	{cost = (6000*0.16)+((power-6000)*(0.20));
    	printf("The cost = %.2lf RS\n",cost);}
    		else
    		 {cost = 6000*0.16;
    	 printf("The cost = %.2lf RS\n",cost);}
    		break ;
    		case 4 :
    			power = current*voltage/1000;
    	 printf("Power =%.2lf kilo watts",power);
    	if (power>6000)
    	{cost = (6000*0.32)+((power-6000)*(0.32));
    	printf("The cost = %.2lf RS\n",cost);}	
    		else
    		{cost = 6000*0.32;
    	printf("The cost = %.2lf RS\n",cost);}
    	    break ;
    	    case 5:
    			power = current*voltage/1000;
    	 printf("Power =%.2lf kilo watts\n",power);
    	    if (power>6000)
    	    {cost = (6000*0.18)+((power-6000)*(0.18));
    	printf("The cost = %.2lf RS\n",cost);}
    		else
    		{cost = 6000*0.18;
    	printf("The cost = %.2lf RS\n",cost);}
    	    break;
    	    case 6 :
    			power = current*voltage/1000;
    	 printf("Power =%.2lf kilo watts\n",power);
    	if (power>6000)
    	{cost = (6000*0.18)+((power-6000)*(0.18));
    	printf("The cost = %.2lf RS\n" ,cost);}
    		else
    		{cost = 6000*0.18;
    	printf("The cost = %.2lf RS \n",cost);}
    	    break;
    		
    	default : printf ("Incorrect Catrgory ! program will stop now ! ");	
    	return 0;
    	}
    if (current==-1||voltage==-1)
    TotalPower = TotalPower + power;
    TotalCost = TotalCost + cost ;
    if (cost<=min)
    min=cost;
    if (cost>=max)
    max=cost;
    	                           times+=1;
    	                      
    }
    while (!(current==-1&&voltage==-1));
    printf ("\nthe total power is : %.2lf \n",TotalPower);
    printf ("\nThe total cost is : %.2lf RS \n" ,TotalCost);
    printf("\nthe min is : %.2lf\n ",min);
    printf("\nthe max is : %.2lf\n",max);
    return 0;
    }

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You initialize min and max, why not your Total variables?

    You need to set min to be some really large value; right now you set it to zero, and since none of your data is lower than that min never changes.

  11. #11
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    A good value to initialize min to...
    Code:
    #include limits.h
    ...
    
    int min = INT_MAX;
    Fact - Beethoven wrote his first symphony in C

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Your indentation continues to be lousy - you'll lose marks.
    Code:
    #include <stdio.h>
    
    int main()
    {
      int times = 1, type;
      double power, cost, TotalPower, TotalCost, max = 0, min = 0, current, voltage;
      do {
        printf("\nRecipient #%d information:\n", times);
        printf("\nInsert current > ");
        scanf("%lf", &current);
        printf("\nInsert voltage > ");
        scanf("%lf", &voltage);
        if (times == 1 && current == -1 && voltage == -1) {
          printf("no recipient information is inserted ");
          return 0;
        }
        printf("\nEnter Consumption Category:\n");
        printf("1 = Residential\n");
        printf("2 = Commercial\n");
        printf("3 = Agricultural & charities\n");
        printf("4 = Government \n");
        printf("5 = Industrial \n");
        printf("6 = Private Educational and Medical\n");
        scanf("%d", &type);
        switch (type) {
        case 1:
          power = current * voltage / 1000;
          printf("Power =%.2lf kilo watts\n", power);
          if (power > 6000) {
            cost = (6000 * 0.18) + ((power - 6000) * (0.3));
            printf("The cost = %.2lf Rs\n", cost);
          } else {
            cost = 6000 * 1.8;
            printf("The cost = %.2lf RS\n", cost);
          }
          break;
        case 2:
          power = current * voltage / 1000;
          printf("Power =%.2lf kilo watts\n", power);
          if (power > 6000) {
            cost = (6000 * 0.20) + ((power - 6000) * (0.30));
            printf("The cost = %.2lf RS\n", cost);
          } else {
            cost = 6000 * 0.2;
            printf("The cost = %.2lf RS\n", cost);
          }
          break;
        case 3:
          power = current * voltage / 1000;
          printf("Power =%.2lf kilo watts", power);
          if (power > 6000) {
            cost = (6000 * 0.16) + ((power - 6000) * (0.20));
            printf("The cost = %.2lf RS\n", cost);
          } else {
            cost = 6000 * 0.16;
            printf("The cost = %.2lf RS\n", cost);
          }
          break;
        case 4:
          power = current * voltage / 1000;
          printf("Power =%.2lf kilo watts", power);
          if (power > 6000) {
            cost = (6000 * 0.32) + ((power - 6000) * (0.32));
            printf("The cost = %.2lf RS\n", cost);
          } else {
            cost = 6000 * 0.32;
            printf("The cost = %.2lf RS\n", cost);
          }
          break;
        case 5:
          power = current * voltage / 1000;
          printf("Power =%.2lf kilo watts\n", power);
          if (power > 6000) {
            cost = (6000 * 0.18) + ((power - 6000) * (0.18));
            printf("The cost = %.2lf RS\n", cost);
          } else {
            cost = 6000 * 0.18;
            printf("The cost = %.2lf RS\n", cost);
          }
          break;
        case 6:
          power = current * voltage / 1000;
          printf("Power =%.2lf kilo watts\n", power);
          if (power > 6000) {
            cost = (6000 * 0.18) + ((power - 6000) * (0.18));
            printf("The cost = %.2lf RS\n", cost);
          } else {
            cost = 6000 * 0.18;
            printf("The cost = %.2lf RS \n", cost);
          }
          break;
    
        default:
          printf("Incorrect Catrgory ! program will stop now ! ");
          return 0;
        }
        if (current == -1 || voltage == -1)
          TotalPower = TotalPower + power;
        TotalCost = TotalCost + cost;
        if (cost <= min)
          min = cost;
        if (cost >= max)
          max = cost;
        times += 1;
    
      }
      while (!(current == -1 && voltage == -1));
      printf("\nthe total power is : %.2lf \n", TotalPower);
      printf("\nThe total cost is : %.2lf RS \n", TotalCost);
      printf("\nthe min is : %.2lf\n ", min);
      printf("\nthe max is : %.2lf\n", max);
      return 0;
    }
    Some of your variable names are meaningless, again - marks lost.
    > printf("\nRecipient #%d information:\n", times);
    Replace 'times' with say 'customerNumber'.


    > THE problem is i have to use the switch here and the function make this more difficult
    Where exactly does it say in the PDF that you have to use a switch statement?

    Sooner or later, you're going to have to start dealing with functions. Putting everything in main() just isn't a sustainable approach.


    Study this latest addition to your code.
    Code:
        if (current == -1 || voltage == -1)
          TotalPower = TotalPower + power;
        TotalCost = TotalCost + cost;
    The scope of the if statement is only the next line.
    Do you really mean to calculate TotalPower on the very last iteration?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with my C++ homework
    By sdoyle in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2013, 07:23 PM
  2. C Homework
    By Cesia in forum C Programming
    Replies: 29
    Last Post: 12-19-2011, 08:44 PM
  3. C homework
    By bringit in forum C Programming
    Replies: 1
    Last Post: 12-17-2011, 01:28 PM
  4. Homework Help
    By xxdustinxx in forum C Programming
    Replies: 14
    Last Post: 11-29-2011, 09:38 AM
  5. Homework?
    By Cloud6 in forum C++ Programming
    Replies: 19
    Last Post: 07-13-2006, 07:51 PM

Tags for this Thread