Thread: Need help in identifying a problem in my code.

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    3

    Question Need help in identifying a problem in my code.

    The problem is simple. I need to enter 4 inputs for each factory (quantity, price etc). So that is 8 inputs in total. Then I need to find the profit these factories each make. After that I need to find the sum of the profits of both factories. Finally I need to show the sum. I can't find the problem in my code. My professor told me I should use double float. Maybe I didn't use double correctly. Help me please!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        double P1,Q1,v1,FC1;
        double P2,Q2,v2,FC2;
        double Pr1,Pr2,AnnPr;
    
    
        P1>=50.00;
        P1<=300.00;
        Q1>=100.00;
        Q1<=50000.00;
        v1>=20.00;
        v1<=1500.00;
        FC1>=3000.00;
        FC1<=50000.00;
        P2>=50.00;
        P2<=300.00;
        Q2>=100.00;
        Q2<=50000.00;
        v2>=20.00;
        v2<=1500.00;
        FC2>=3000.00;
        FC2<=50000.00;
    
    
    
    
        scanf("%lf%lf%lf%lf",&P1,&Q1,&v1,&FC1);
        scanf("%lf%lf%lf%lf",&P2,&Q2,&v2,&FC2);
    
    
        Pr1=P1*Q1-(v1*Q1-FC1);
        Pr2=P2*Q2-(v2*Q2-FC2);
    
    
        AnnPr=Pr1+Pr2;
    
    
        printf("%lf",AnnPr);
    
    
    
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2016
    Posts
    3
    Try these inputs.
    P1=50
    Q1=100
    v1=20
    FC1=3000

    P2=1400
    Q2=7840
    v2=860
    FC2=17548

    THE SUM SHOULD BE=4216052

  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
    Why do you think the answer should be what you say?

    All your <= comparisons (lines 12 to 27 in your post) serve no purpose whatsoever.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    int main()
    {
        double P1,Q1,v1,FC1;
        double P2,Q2,v2,FC2;
        double Pr1,Pr2,AnnPr;
     
        scanf("%lf%lf%lf%lf",&P1,&Q1,&v1,&FC1);
        scanf("%lf%lf%lf%lf",&P2,&Q2,&v2,&FC2);
     
        Pr1=P1*Q1-(v1*Q1-FC1);
        Pr2=P2*Q2-(v2*Q2-FC2);
        AnnPr=Pr1+Pr2;
     
        printf("Pr1=%lf\n",Pr1);
        printf("Pr2=%lf\n",Pr2);
        printf("AnnPr=%lf\n",AnnPr);
     
        return 0;
    }
    
    $ gcc -Wall main.c
    $ ./a.out 
    50 100 20 3000
    1400 7840 860 17548
    Pr1=6000.000000
    Pr2=4251148.000000
    AnnPr=4257148.000000
    So is it Pr1 or Pr2 which is wrong in your estimation?

    Failing that, try using the debugger to examine the program state in finer detail.
    Code:
    $ gcc -Wall -g main.c
    $ gdb -q ./a.out
    Reading symbols from ./a.out...done.
    (gdb) b 17
    Breakpoint 1 at 0x4006c2: file main.c, line 17.
    (gdb) run
    Starting program: /home/sc/Documents/a.out 
    50 100 20 3000
    1400 7840 860 17548
    
    Breakpoint 1, main () at main.c:17
    17	    printf("Pr1=%lf\n",Pr1);
    (gdb) info locals
    P1 = 50
    Q1 = 100
    v1 = 20
    FC1 = 3000
    P2 = 1400
    Q2 = 7840
    v2 = 860
    FC2 = 17548
    Pr1 = 6000
    Pr2 = 4251148
    AnnPr = 4257148
    (gdb) c
    Continuing.
    Pr1=6000.000000
    Pr2=4251148.000000
    AnnPr=4257148.000000
    [Inferior 1 (process 2961) exited normally]
    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
    Registered User
    Join Date
    Dec 2016
    Posts
    3
    Thanks for the reply. I found the problem. It didn't show me the correct output because I made a mistake in the formula. It should've been: P1*Q1 - (v1*Q1 + FC1).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-07-2012, 04:06 PM
  2. Identifying a C++ idiom
    By kovacsbv in forum C++ Programming
    Replies: 2
    Last Post: 08-10-2009, 02:50 PM
  3. Identifying Printer Model
    By chinesepirate in forum Windows Programming
    Replies: 1
    Last Post: 06-24-2009, 03:46 PM
  4. Identifying Triangles
    By howeezy in forum C++ Programming
    Replies: 5
    Last Post: 10-08-2005, 12:14 PM
  5. Identifying Who Logged In & getlogin()
    By Crashgr in forum Linux Programming
    Replies: 12
    Last Post: 12-25-2004, 09:01 PM

Tags for this Thread