Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Need help with this little C program

Need help with this little C program

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
13 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 11946215
    wrote on last edited by
    #1

    Hey guys, I have to write a small program in C, I'll explain what it has to do. You have product a, b and c The input is the prices of a, b and c in cents. You have to spend exactly 100 euros on the purchase of exactly 100 products. As output I need to get a list of all possible combinations. Lets give an example. Lets say the input is "88 99 102": then the output has to be exactly like this: 1 a, 62 b, 37 c 4 a, 48 b, 48 c 7 a, 34 b, 59 c 10 a, 20 b, 70 c 13 a, 6 b, 81 c I'm relatively new in progamming in C, so all help is appreciated, the deadline is short for this one.

    int main(int argc, char *argv[]) {
    int a, b, c, x, y, z /* x=numbers of a y= numbers of b z= numbers of c*/
    printf("price of a:\n");
    printf("price of b:\n");
    printf("price of c:\n");
    scanf("%d, %d, %d" ,&a, &b, &c);
    while (x+y+z=100){
    x*a+y*b+z*c=10000

    As you can see, I tried writing some code, but im stuck right here...

    L CPalliniC U D 4 Replies Last reply
    0
    • U User 11946215

      Hey guys, I have to write a small program in C, I'll explain what it has to do. You have product a, b and c The input is the prices of a, b and c in cents. You have to spend exactly 100 euros on the purchase of exactly 100 products. As output I need to get a list of all possible combinations. Lets give an example. Lets say the input is "88 99 102": then the output has to be exactly like this: 1 a, 62 b, 37 c 4 a, 48 b, 48 c 7 a, 34 b, 59 c 10 a, 20 b, 70 c 13 a, 6 b, 81 c I'm relatively new in progamming in C, so all help is appreciated, the deadline is short for this one.

      int main(int argc, char *argv[]) {
      int a, b, c, x, y, z /* x=numbers of a y= numbers of b z= numbers of c*/
      printf("price of a:\n");
      printf("price of b:\n");
      printf("price of c:\n");
      scanf("%d, %d, %d" ,&a, &b, &c);
      while (x+y+z=100){
      x*a+y*b+z*c=10000

      As you can see, I tried writing some code, but im stuck right here...

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      This is not a programming problem, more one of mathematics. You first need to work out the algorithm to calculate the numbers of each product that you can buy for €100. Once you have that then turning it into code should be fairly trivial.

      1 Reply Last reply
      0
      • U User 11946215

        Hey guys, I have to write a small program in C, I'll explain what it has to do. You have product a, b and c The input is the prices of a, b and c in cents. You have to spend exactly 100 euros on the purchase of exactly 100 products. As output I need to get a list of all possible combinations. Lets give an example. Lets say the input is "88 99 102": then the output has to be exactly like this: 1 a, 62 b, 37 c 4 a, 48 b, 48 c 7 a, 34 b, 59 c 10 a, 20 b, 70 c 13 a, 6 b, 81 c I'm relatively new in progamming in C, so all help is appreciated, the deadline is short for this one.

        int main(int argc, char *argv[]) {
        int a, b, c, x, y, z /* x=numbers of a y= numbers of b z= numbers of c*/
        printf("price of a:\n");
        printf("price of b:\n");
        printf("price of c:\n");
        scanf("%d, %d, %d" ,&a, &b, &c);
        while (x+y+z=100){
        x*a+y*b+z*c=10000

        As you can see, I tried writing some code, but im stuck right here...

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        A brute force approach would work, e.g.

        for ( x = 1; x <= 98; x++)
        for ( y = 1; y <= (99 - x); ++y)
        {
        z = 100 - x - y;
        // here check if the items sum to 100
        }

        In testa che avete, signor di Ceprano?

        U 1 Reply Last reply
        0
        • CPalliniC CPallini

          A brute force approach would work, e.g.

          for ( x = 1; x <= 98; x++)
          for ( y = 1; y <= (99 - x); ++y)
          {
          z = 100 - x - y;
          // here check if the items sum to 100
          }

          U Offline
          U Offline
          User 11946215
          wrote on last edited by
          #4

          Thanks. I'm finally getting the hang of it. I'll try something tomorrow and I will let you peoples know if I succeed or not.

          CPalliniC 1 Reply Last reply
          0
          • U User 11946215

            Hey guys, I have to write a small program in C, I'll explain what it has to do. You have product a, b and c The input is the prices of a, b and c in cents. You have to spend exactly 100 euros on the purchase of exactly 100 products. As output I need to get a list of all possible combinations. Lets give an example. Lets say the input is "88 99 102": then the output has to be exactly like this: 1 a, 62 b, 37 c 4 a, 48 b, 48 c 7 a, 34 b, 59 c 10 a, 20 b, 70 c 13 a, 6 b, 81 c I'm relatively new in progamming in C, so all help is appreciated, the deadline is short for this one.

            int main(int argc, char *argv[]) {
            int a, b, c, x, y, z /* x=numbers of a y= numbers of b z= numbers of c*/
            printf("price of a:\n");
            printf("price of b:\n");
            printf("price of c:\n");
            scanf("%d, %d, %d" ,&a, &b, &c);
            while (x+y+z=100){
            x*a+y*b+z*c=10000

            As you can see, I tried writing some code, but im stuck right here...

            U Offline
            U Offline
            User 11946215
            wrote on last edited by
            #5

            Thank you guys. I can't compile and execute right now. Will this give me the output I want? If not, where did I go wrong?

            #include #include #include int main(int argc, char *argv[])
            {
            int a, b, c, x, y, z; /* x=numbers of a y= numbers of b z= numbers of c*/
            printf("price of a:\n");
            printf("price of b:\n");
            printf("price of c:\n");
            scanf("%d, %d, %d" ,&a, &b, &c);

            for(x = 1; x <= 100; x++)
            {
            	for(y = 1; y <= (100 - x); ++y)
            	{
            		z = 100 - x - y;
            		if(x\*a + y\*b + z\*c == 100)
            		{
                		printf("x %d, y %d, z %d",a ,b ,c);
            		}
            	}
            }
            

            return 0;
            }

            1 Reply Last reply
            0
            • U User 11946215

              Thanks. I'm finally getting the hang of it. I'll try something tomorrow and I will let you peoples know if I succeed or not.

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              You are welcome.

              In testa che avete, signor di Ceprano?

              U 1 Reply Last reply
              0
              • CPalliniC CPallini

                You are welcome.

                U Offline
                U Offline
                User 11946215
                wrote on last edited by
                #7

                Thanks again, I make improve in the code, but it ain't working as supposed. I'm really wondering where the code is wrong, I get no output or a wrong output (e.g. 100 100 100 gives as output only 100 a, 0 b, 0 c.) I tried different things and I guess I'm almost there, but can't fix it. So please, help me with this code.

                #include #include #include int main(int argc, char *argv[])
                {
                int a, b, c, x, y, z; /* x=numbers of a y= numbers of b z= numbers of c*/
                printf("price of a:\n");
                printf("price of b:\n");
                printf("price of c:\n");
                scanf("%d, %d, %d" ,&a, &b, &c);

                for(x = 0; x <= 100; x++)
                {
                	for(y = 0; y <= (100 - x); ++y)
                	{
                		        z = 100 - x - y;
                		        if(x\*a + y\*b + z\*c == 10000)
                		        {
                    		      printf("%d a, %d b, %d c",x ,y ,z);
                		        }
                	}
                }
                

                return 0;
                }

                CPalliniC 1 Reply Last reply
                0
                • U User 11946215

                  Thanks again, I make improve in the code, but it ain't working as supposed. I'm really wondering where the code is wrong, I get no output or a wrong output (e.g. 100 100 100 gives as output only 100 a, 0 b, 0 c.) I tried different things and I guess I'm almost there, but can't fix it. So please, help me with this code.

                  #include #include #include int main(int argc, char *argv[])
                  {
                  int a, b, c, x, y, z; /* x=numbers of a y= numbers of b z= numbers of c*/
                  printf("price of a:\n");
                  printf("price of b:\n");
                  printf("price of c:\n");
                  scanf("%d, %d, %d" ,&a, &b, &c);

                  for(x = 0; x <= 100; x++)
                  {
                  	for(y = 0; y <= (100 - x); ++y)
                  	{
                  		        z = 100 - x - y;
                  		        if(x\*a + y\*b + z\*c == 10000)
                  		        {
                      		      printf("%d a, %d b, %d c",x ,y ,z);
                  		        }
                  	}
                  }
                  

                  return 0;
                  }

                  CPalliniC Offline
                  CPalliniC Offline
                  CPallini
                  wrote on last edited by
                  #8

                  You don't use correctly scanf. Try

                  #include <stdio.h>
                  #include <stdlib.h>
                  #include <math.h>

                  int main(int argc, char *argv[])
                  {
                  int a, b, c, x, y, z; /* x=numbers of a y= numbers of b z= numbers of c*/

                  int done = 0;

                  while ( done == 0)
                  {
                  printf("please enter the price of a:\n");
                  if ( scanf("%d", &a) != 1) continue;
                  printf("please enter the price of b:\n");
                  if ( scanf("%d", &b) != 1) continue;
                  printf("please enter the price of c:\n");
                  if ( scanf("%d", &c) != 1) continue;
                  done = 1;
                  }

                  for(x = 0; x <= 100; x++)
                  {
                  for(y = 0; y <= (100 - x); ++y)
                  {
                  z = 100 - x - y;
                  if(x*a + y*b + z*c == 10000)
                  {
                  printf("%d a, %d b, %d c\n",x ,y ,z);
                  }
                  }
                  }

                  return 0;
                  }

                  In testa che avete, signor di Ceprano?

                  U 1 Reply Last reply
                  0
                  • CPalliniC CPallini

                    You don't use correctly scanf. Try

                    #include <stdio.h>
                    #include <stdlib.h>
                    #include <math.h>

                    int main(int argc, char *argv[])
                    {
                    int a, b, c, x, y, z; /* x=numbers of a y= numbers of b z= numbers of c*/

                    int done = 0;

                    while ( done == 0)
                    {
                    printf("please enter the price of a:\n");
                    if ( scanf("%d", &a) != 1) continue;
                    printf("please enter the price of b:\n");
                    if ( scanf("%d", &b) != 1) continue;
                    printf("please enter the price of c:\n");
                    if ( scanf("%d", &c) != 1) continue;
                    done = 1;
                    }

                    for(x = 0; x <= 100; x++)
                    {
                    for(y = 0; y <= (100 - x); ++y)
                    {
                    z = 100 - x - y;
                    if(x*a + y*b + z*c == 10000)
                    {
                    printf("%d a, %d b, %d c\n",x ,y ,z);
                    }
                    }
                    }

                    return 0;
                    }

                    U Offline
                    U Offline
                    User 11946215
                    wrote on last edited by
                    #9

                    Thank you so much! It works great. I had to change the code a bit to pass all testcases, but finally I made it :). However, I'd like to know how that piece of code between the While { ... } works, I don't really get it. done = 0, so while ( done == 0) is true. Then it runs the code and you can enter the values. But why does it need the done == 0 and why the != 1? I understand the done = 1, otherwise it will run the while statement again, right?

                    U 1 Reply Last reply
                    0
                    • U User 11946215

                      Thank you so much! It works great. I had to change the code a bit to pass all testcases, but finally I made it :). However, I'd like to know how that piece of code between the While { ... } works, I don't really get it. done = 0, so while ( done == 0) is true. Then it runs the code and you can enter the values. But why does it need the done == 0 and why the != 1? I understand the done = 1, otherwise it will run the while statement again, right?

                      U Offline
                      U Offline
                      User 11946215
                      wrote on last edited by
                      #10

                      Well, I don't know why it did not work the first time, but I tried to simplify the code and ended up with: printf("price of a: price of b: price of c: "); scanf("%d %d %d", &a, &b, &c); instead of that thing with while { ... } And this time, it did work. I do not really see the difference with the code I used before that did not work.. :p

                      CPalliniC 1 Reply Last reply
                      0
                      • U User 11946215

                        Well, I don't know why it did not work the first time, but I tried to simplify the code and ended up with: printf("price of a: price of b: price of c: "); scanf("%d %d %d", &a, &b, &c); instead of that thing with while { ... } And this time, it did work. I do not really see the difference with the code I used before that did not work.. :p

                        CPalliniC Offline
                        CPalliniC Offline
                        CPallini
                        wrote on last edited by
                        #11

                        Even the code you used before would have work with a suitable input, that is if the user would have entered, for instance

                        100,100,100

                        that is, with the commas. However, the code handling input from the user should be more robust.

                        In testa che avete, signor di Ceprano?

                        1 Reply Last reply
                        0
                        • U User 11946215

                          Hey guys, I have to write a small program in C, I'll explain what it has to do. You have product a, b and c The input is the prices of a, b and c in cents. You have to spend exactly 100 euros on the purchase of exactly 100 products. As output I need to get a list of all possible combinations. Lets give an example. Lets say the input is "88 99 102": then the output has to be exactly like this: 1 a, 62 b, 37 c 4 a, 48 b, 48 c 7 a, 34 b, 59 c 10 a, 20 b, 70 c 13 a, 6 b, 81 c I'm relatively new in progamming in C, so all help is appreciated, the deadline is short for this one.

                          int main(int argc, char *argv[]) {
                          int a, b, c, x, y, z /* x=numbers of a y= numbers of b z= numbers of c*/
                          printf("price of a:\n");
                          printf("price of b:\n");
                          printf("price of c:\n");
                          scanf("%d, %d, %d" ,&a, &b, &c);
                          while (x+y+z=100){
                          x*a+y*b+z*c=10000

                          As you can see, I tried writing some code, but im stuck right here...

                          D Offline
                          D Offline
                          David Crow
                          wrote on last edited by
                          #12

                          Perhaps your problem can be broken up into two smaller problems: 1) Find all combinations of A+B+C that equal (quantity) 100. I *think* that gives you a set of about 4851 combinations. 2) For each of those, multiply A by 0.88, b by 0.99, and C by 1.02, and if that product equals 100 euros, you have a match.

                          "One man's wage rise is another man's price increase." - Harold Wilson

                          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                          "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                          U 1 Reply Last reply
                          0
                          • D David Crow

                            Perhaps your problem can be broken up into two smaller problems: 1) Find all combinations of A+B+C that equal (quantity) 100. I *think* that gives you a set of about 4851 combinations. 2) For each of those, multiply A by 0.88, b by 0.99, and C by 1.02, and if that product equals 100 euros, you have a match.

                            "One man's wage rise is another man's price increase." - Harold Wilson

                            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                            "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                            U Offline
                            U Offline
                            User 11946215
                            wrote on last edited by
                            #13

                            That sounds like a faster way to calculate, because of the smaller set of combinations. I'd like to try that another time, but I don't have the time to do that now before the deadline. Thanks for the tip, nice to see how there are multiple solutions to work it out.

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups