Thread: How to get the Sum

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    3

    How to get the Sum

    So i am struggling for days to get this done and all i need is to get the sum of the areas and get them printed on the screen my code is this:

    Code:
    #include<stdio.h>
    #include <stdlib.h>
    #include<conio.h>  //Not needed in Dev C++//
    #define PI 3.1415 
    float Area_of_Rectangular(float  length,float width);
    float Area_of_Circle(float radius);
    int main()
    {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
       int rooms,i;
       float length1,length2,width1,width2,aor,radius1,radius2,aoc;
       char answer;
       printf("Please enter the number of the rooms:");
       scanf("%d",&rooms);
       for(i=0;i<rooms;i++){                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
                            printf("Is room#%d in a rectangular shape?(y/n)\n",i+1);
                            scanf(" %c",&answer); //note leading space to skip any whitespace, and thus read the next non-space character.//
                            if (answer == 'y' || answer == 'Y'){                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
                                                                printf("Give length for room#%d:",i+1);
                                                                scanf("%f",&length1);
                                                                printf("Give width for room#%d:",i+1);
                                                                scanf("%f",&width1);
                                                                aor=Area_of_Rectangular(length1,width1);
                                                                printf("The area of the rectangle room#%d is :%f\n",i+1,aor);
                                                                printf("Does room#%d have a balcony?(y/n)\n",i+1);
                                                                scanf(" %c",&answer);
                                                                if (answer == 'y' || answer == 'Y'){                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
                                                                                                    printf("Is the balcony of room#%d in a rectangular shape?(y/n)\n",i+1);
                                                                                                    scanf(" %c",&answer);
                                                                                                    if (answer == 'y' || answer == 'Y'){                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                        printf("Give length for balcony of room#%d:",i+1);
                                                                                                                                        scanf("%f",&length2);
                                                                                                                                        printf("Give width for balcony of room#%d:",i+1);
                                                                                                                                        scanf("%f",&width2);
                                                                                                                                        aor=Area_of_Rectangular(length2,width2);
                                                                                                                                        if(aor<width1){                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
                                                                                                                                                                     printf("The area of the rectangular balcony of room#%d is :%f ",i+1,aor);
                                                                                                                                                                     }                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
                                                                                                                                                                     else{                                                                                                                                                                                                                                                                                                                                                                                                                                                             
                                                                                                                                                                          printf("Sorry but the size of the balcony cannot exceed the width and/or length of the room\n");
                                                                                                                                                                          }                                                                                                                                                                                                                                                                                                                                                                                                                                                             
                                                                                                                                        }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
                                                                                                                                        else if (answer == 'n' || answer == 'N'){                                                                                                                                                                                                                                                                                                                                                                                                                                                      
                                                                                                                                                                                 printf("Is the balcony of room#%d in a semicircular shape?(y/n)\n",i+1);
                                                                                                                                                                                 scanf(" %c",&answer);
                                                                                                                                                                                 if (answer == 'y' || answer == 'Y'){                                                                                                                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                     printf("Give radius for balcony of room#%d: ",i+1);
                                                                                                                                                                                                                     scanf("%f",&radius2);
                                                                                                                                                                                                                     aoc=Area_of_Circle(radius2)/2.0; //note:We divide the Area of Circle so we can take the Area of the Semicircle//
                                                                                                                                                                                                                     if(radius2*2<width1){                                                                                                                                                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                          printf("The area of the semicircular balcony of room#%d is :%f ",i+1,aoc);
                                                                                                                                                                                                                                          }                                                                                                                                                                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                          else{                                                                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                               printf("Sorry but the size of the balcony cannot exceed the width of the room\n");
                                                                                                                                                                                                                                               }                                                                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                     }                                                                                                                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                     else if(answer == 'n' || answer == 'N'){                                                                                                                                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                             printf("Sorry but we can't give any other choices for you to check the area of your balcony.Check back for more updates at www.*****.com.Thank you for using this program\n");
                                                                                                                                                                                                                                                             }                                                                                                                                                                                                                                                                                                                                                                           
                                                                                                                                                                                  }                                                                                                                                                                                                                                                                                                                                                                                                                                                       
                                                                                                     }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
                                                                   }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
                                                                   else if (answer == 'n' || answer == 'N'){                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
                                                                                                            printf("Is room#%d in a circular shape?(y/n)\n",i+1);
                                                                                                            scanf(" %c",&answer);
                                                                                                            if (answer == 'y' || answer == 'Y'){                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
                                                                                                                                                printf("Enter the radius for room#%d:",i+1);
                                                                                                                                                scanf("%f",&radius1);
                                                                                                                                                aoc=Area_of_Circle(radius1);
                                                                                                                                                printf("The area of the circular room#%d is :%f\n",i+1,aoc);
                                                                                                                                                printf("Does room#%d have a balcony?(y/n)\n",i+1);
                                                                                                                                                scanf(" %c",&answer);
                                                                                                                                                if (answer == 'y' || answer == 'Y'){                                                                                                                                                                                                                                                                                                                                                                                                                                                     
                                                                                                                                                                                    printf("Is the balcony of room#%d in a rectangular shape?(y/n)\n",i+1);
                                                                                                                                                                                    scanf(" %c",&answer);
                                                                                                                                                                                    if (answer == 'y' || answer == 'Y'){                                                                                                                                                                                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                        printf("Give length for balcony of room#%d:",i+1);
                                                                                                                                                                                                                        scanf("%f",&length2);
                                                                                                                                                                                                                        printf("Give width for balcony of room#%d:",i+1);
                                                                                                                                                                                                                        scanf("%f",&width2);
                                                                                                                                                                                                                        aor=Area_of_Rectangular(length2,width2);
                                                                                                                                                                                                                        if((width2<radius1)&&(length2<2*radius1)){                                                                                                                                                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                 printf("The area of the rectangular balcony of room#%d is :%f ",i+1,aor);
                                                                                                                                                                                                                                                                 }                                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                 else{                                                                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                      printf("Sorry but the width of the balcony cannot exceed the radius of the room and/or the length cannot exceed it's diameter\n");
                                                                                                                                                                                                                                                                      }                                                                                                                                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                        }                                                                                                                                                                                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                        else if (answer == 'n' || answer == 'N'){                                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                 printf("Is the balcony of room#%d in a semicircular shape?(y/n)\n",i+1);
                                                                                                                                                                                                                                                                 scanf(" %c",&answer);
                                                                                                                                                                                                                                                                 if (answer == 'y' || answer == 'Y'){                                                                                                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                                                     printf("Give radius for balcony of room#%d: ",i+1);
                                                                                                                                                                                                                                                                                                     scanf("%f",&radius2);
                                                                                                                                                                                                                                                                                                     aoc=Area_of_Circle(radius2)/2;
                                                                                                                                                                                                                                                                                                     if(radius2<radius1){                                                                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                         printf("The area of the semicircular balcony of room#%d is :%f ",i+1,aoc);
                                                                                                                                                                                                                                                                                                                         }                                                                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                                                                         else{                                                                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                                                                              printf("Sorry but the radius of the balcony cannot exceed the radius of the room\n");
                                                                                                                                                                                                                                                                                                                              }                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                     }                                                                                                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                                                                     else if (answer == 'n' || answer == 'N'){                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                              printf("Sorry but we can't give any other choices for you to check the area of your balcony.Check back for more updates at www.*****.com.Thank you for using this program\n");
                                                                                                                                                                                                                                                                                                                                              }                                                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                 }                                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                     }                                                                                                                                                                                                                                                                                                                                                                                                                                                   
                                                                                                                                                                                     else if (answer == 'n' || answer == 'N'){                                                                                                                                                                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                              printf("\n");
                                                                                                                                                                                                                              }                                                                                                                                                                                                                                                                                                                                                                                                           
                                                                                                                                                  }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
                                                                                                                                                  else if(answer == 'n' || answer == 'N'){                                                                                                                                                                                                                                                                                                                                                                                                                                                
                                                                                                                                                                                          printf("Sorry but we can't give any other choices for you to check the area of your room.Check back for more updates at www.*****.com.Thank you for using this program\n");
                                                                                                                                                                                          }                                                                                                                                                                                                                                                                                                                                                                                                                                               
                                                                                                             }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
                            }                                                                                                                                                                                                                                                                                                                 
    getch();                           
    system("PAUSE");
    return 0;
    }
    
    float Area_of_Rectangular(float  length,float width)
    {
          float area_of_rect_room;
          area_of_rect_room=length*width;
          return(area_of_rect_room);
    }
    
    float Area_of_Circle(float radius)
    {
          float area_of_circ_room;
          area_of_circ_room=PI*radius*radius;
          return(area_of_circ_room);
    }


    Sorry if it's too messed up just made it like this to help me make the chains with every if i made.Feel free to give me a better view of it also

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Pass a pointer to aor to the function Area_of_Rectangular() and a pointer to aoc to Area_of_Circle() and print whatever the functions return?

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    3
    Sorry but i didn't get that(beginner material) How can i pass a pointer to the functions and where do i do that where i declare them or when i give them their types?

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Angelusmorts View Post
    Sorry but i didn't get that(beginner material) How can i pass a pointer to the functions and where do i do that where i declare them or when i give them their types?
    Err actually, you don't need a pointer.

    The function returns the calculated area so just assign that to your variable.

    An example would be:

    Code:
    int i;
    int input;
    
    input = 5;
    i = functionthatdoessomething(input);
    
    printf("%d\n", i);
    or, if you don't need to "save" the result:

    Code:
    int input;
    
    input = 5;
    printf("%d\n", functionthatdoessomething(input));

  5. #5
    Registered User
    Join Date
    Mar 2014
    Posts
    3
    Sorry i did try it but i am not getting the result and i am still uncertain how this will work since i believe i would have to add the same lines of code(for the sum)everywhere i ask for the Area of rectangular/circular/semicircular.Can some1 else share his thoughts?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think that you should indent your code properly. It is conventional for an indent level to be either one tab, or fixed number of spaces (two to eight).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Angelusmorts View Post
    Sorry i did try it but i am not getting the result and i am still uncertain how this will work since i believe i would have to add the same lines of code(for the sum)everywhere i ask for the Area of rectangular/circular/semicircular.Can some1 else share his thoughts?
    Yes, so store the sum in its own variable

    Code:
    int sum;
    
    sum = 0;
    
    // ...
    
    sum += Area_of_whatever();
    
    // ...
    
    sum += Area_of_whatever();
    
    // maybe the above are in a loop... makes no difference
    
    // after everything has been added to sum, print the result

  8. #8
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by laserlight View Post
    I think that you should indent your code properly. It is conventional for an indent level to be either one tab, or fixed number of spaces (two to eight).
    What? You don't have 3 monitors and maximise every window you open? Odd.

Popular pages Recent additions subscribe to a feed