C programing help on a problem set

C programing help on a problem set

Post by Teh-brain-eater on Thu Dec 22, 2011 12:26 pm
([msg=63366]see C programing help on a problem set[/msg])

Hello to all,
Im having difficulties on the first problem in this pset1. Specifically pennies.c.
So far I have done the first two requirements in the pset but Im stuck on the part where I have to double the pennies with the number of days the user inputs. I know it has to be a for loop and I also think is a combination of the while loop as well but I am not certain. I have been stuck for this problem for numerous days! I have tried working on this myself for while and Im finally asking for help on this one... Here is my code, I know asking for the days and pennies are right. The pennies pset is on pages 8 and 9<16. Thanks to all.

http://cdn.cs50.net/2011/fall/psets/1/pset1.pdf


Code: Select all
#include <cs50.h>
#include <stdio.h>

int
main (void)
{
    /* Gets the number of days in a month. */
    long long days;
    do
  {
     printf ("How many days in a month: ");
     days = GetInt();
  }
      while ((28>days) || (31 < days)); /* Checks if the users input is valid. */

    /* Gets the number of pennies in the first day */
   long long pens;
   do
  {
    printf ("How many pennies will you receive the first day: ");
    pens = GetInt();
  }
     while (pens <= 0); /* Checks if the users input is valid. */

  for (days = 1; days <= 31; days *=2);

    long long total1;
{
    for (pens = 0; pens <=31; pens *=2);
    total1 = pens * days;
}

   long long total2;
{
   total2 = pens *= days * total1;
   printf ("$%.2lld", total 2);
}


}
Teh-brain-eater
New User
New User
 
Posts: 12
Joined: Thu Dec 22, 2011 12:01 pm
Blog: View Blog (0)


Re: C programing help on a problem set

Post by LoGiCaL__ on Thu Dec 22, 2011 1:28 pm
([msg=63367]see Re: C programing help on a problem set[/msg])

This part is where your problem is:

Code: Select all
  for (days = 1; days <= 31; days *=2);

    long long total1;
{
    for (pens = 0; pens <=31; pens *=2);
    total1 = pens * days;
}

   long long total2;
{
   total2 = pens *= days * total1;
   printf ("$%.2lld", total 2);
}


First off, once days is set you don't want days to change. make another variable called iterator so it knows when it has reached the day limit. 1 because of day one.

Second you need total to equal pens * 2 to increase the amount of pennies by two times for each iteration.

Third you need to assign the new value to pennies, so it keeps the updated value for the next iteration.

Here is what I would do, instead of the previous loops. Note: I'm used to C++ so there may be subtle differences, however, just pay attention to the logic.

Code: Select all
int days = 30;   //<---This will be User Input. I Initiated for quickness
long pens = 1, total;
int iterator;

for(iterator = 1 ; iterator <= days; ++iterator)
{
    total = pens * 2;
    pens = total;
}

std::cout << total << std::endl;
User avatar
LoGiCaL__
Addict
Addict
 
Posts: 1061
Joined: Sun May 30, 2010 12:33 pm
Blog: View Blog (0)


Re: C programing help on a problem set

Post by runner7gk on Thu Dec 22, 2011 3:28 pm
([msg=63368]see Re: C programing help on a problem set[/msg])

i fully agree with logical.

your userinput is done correctly.

but loops are quite messed up. first you have to remove the semikolons in the line where you start your for-loops!! in c it is nonesense. also the curly brakets are set wrong. take a look at basic syntax of the c language.
Here i just corrected (or better tried to interprete) the syntax in your code. The logic is still wrong.

Code: Select all

long long total2;
long long total1;

for (days = 1; days <= 31; days *=2){

    for (pens = 0; pens <=31; pens *=2){
        total1 = pens * days;
    }
}
   total2 = pens *= days * total1;
   printf ("$%.2lld", total 2);


instead of runnig through the whole 28,29,30 or 31 days in a loop, you are setting them to 1. actually you are ereasing the userinput there. while being doubled each round your first loop runs always only 6 times (value of days is 32 then).

each time (6 in total) you are runnig through another loop and multiply the values of your variables. try to imagine what the programm is actually doing:
value of days = 1 --> go in 2nd loop, value of pens = 0, total = 0*1 --> total =0, pens*=2 --> pens = pens * 2 -->
pens = 0 * 2 = 0
you can see, because you are always multiplying by 0 it is an infinite loop. so you´re stucked there.

logical posted you the solution. iterator is just another name for your i and the statement i++ or ++i does not differ in c. the correct output you already have in youre code. just change it because cout is a c++ statement.
runner7gk
New User
New User
 
Posts: 6
Joined: Sun Dec 18, 2011 7:42 am
Blog: View Blog (0)


Re: C programing help on a problem set

Post by Teh-brain-eater on Thu Dec 22, 2011 5:29 pm
([msg=63376]see Re: C programing help on a problem set[/msg])

Thanks logical! Thanks runner7gk! I know my for loop was wrong thats why I was asking for help on it but thanks for the input you guys! Im thrilled now that I know how to double a number for each day etc. Everything is good but there is a tiny piece missing to this problem. How do I make the last 2 numbers trail in cents? I tried doing
Code: Select all
printf (" $%.2lld\n", total);

I get the total and the dollar sign but I can't make the last 2 digits in cents.
Teh-brain-eater
New User
New User
 
Posts: 12
Joined: Thu Dec 22, 2011 12:01 pm
Blog: View Blog (0)


Re: C programing help on a problem set

Post by LoGiCaL__ on Thu Dec 22, 2011 6:14 pm
([msg=63377]see Re: C programing help on a problem set[/msg])

Teh-brain-eater wrote:Thanks logical! Thanks runner7gk! I know my for loop was wrong thats why I was asking for help on it but thanks for the input you guys! Im thrilled now that I know how to double a number for each day etc. Everything is good but there is a tiny piece missing to this problem. How do I make the last 2 numbers trail in cents? I tried doing
Code: Select all
printf (" $%.2lld\n", total);

I get the total and the dollar sign but I can't make the last 2 digits in cents.


http://www.cplusplus.com/reference/clibrary/cstdio/printf/
User avatar
LoGiCaL__
Addict
Addict
 
Posts: 1061
Joined: Sun May 30, 2010 12:33 pm
Blog: View Blog (0)


Re: C programing help on a problem set

Post by runner7gk on Fri Dec 23, 2011 3:08 am
([msg=63379]see Re: C programing help on a problem set[/msg])



maybe one more hint: you are trying to printf a floating point number with a variable which isn´t declared as such. so the problem is not only your printf. also take a look at your declaration of pens and total.
runner7gk
New User
New User
 
Posts: 6
Joined: Sun Dec 18, 2011 7:42 am
Blog: View Blog (0)


Re: C programing help on a problem set

Post by Teh-brain-eater on Tue Dec 27, 2011 12:07 pm
([msg=63427]see Re: C programing help on a problem set[/msg])

Thanks guys but I've already done everything you have suggested to me. It does look like a dollar and cents problems now but those trailing zeros are not what I have been hoping for. Here is my code.

Code: Select all
 
double pens, total;
do
{
      printf ("How many pennies will you receive the first day: ");
      pens = GetInt();
}
   while ( pens <=0); /* Checks if the users input is valid */

   double iterator;

   for (iterator = 1; iterator <= days; ++iterator)
      {
               total = pens * 2;
               pens = total;
      }
           printf ("$%.2f\n", total);
       


*Note: This is not my full code I just broke it down because the main problem is the printf section.*
I want the decimal to break within the last 2 digits not after the last 2 digits. I have tried numerous variations with the declarations of double and float but nothing. I still get trailing zeros.

Here is a example

How many days in a month: 31
How many pennies will you receive the first day: 1
$2147483648.00

^ How do I get the decimal between the 6 and the 4?
Teh-brain-eater
New User
New User
 
Posts: 12
Joined: Thu Dec 22, 2011 12:01 pm
Blog: View Blog (0)


Re: C programing help on a problem set

Post by LoGiCaL__ on Tue Dec 27, 2011 12:46 pm
([msg=63429]see Re: C programing help on a problem set[/msg])

Teh-brain-eater wrote:Thanks guys but I've already done everything you have suggested to me. It does look like a dollar and cents problems now but those trailing zeros are not what I have been hoping for. Here is my code.

Code: Select all
 
double pens, total;
do
{
      printf ("How many pennies will you receive the first day: ");
      pens = GetInt();
}
   while ( pens <=0); /* Checks if the users input is valid */

   double iterator;

   for (iterator = 1; iterator <= days; ++iterator)
      {
               total = pens * 2;
               pens = total;
      }
           printf ("$%.2f\n", total);
       


*Note: This is not my full code I just broke it down because the main problem is the printf section.*
I want the decimal to break within the last 2 digits not after the last 2 digits. I have tried numerous variations with the declarations of double and float but nothing. I still get trailing zeros.

Here is a example

How many days in a month: 31
How many pennies will you receive the first day: 1
$2147483648.00

^ How do I get the decimal between the 6 and the 4?


You can try dividing by 100. If your original amount is 2147483648.00.

Code: Select all
printf("%.2f\n", (amount / 100));
User avatar
LoGiCaL__
Addict
Addict
 
Posts: 1061
Joined: Sun May 30, 2010 12:33 pm
Blog: View Blog (0)


Re: C programing help on a problem set

Post by runner7gk on Tue Dec 27, 2011 5:03 pm
([msg=63437]see Re: C programing help on a problem set[/msg])

i don´t get why you want to set your decimal between 6 and 4??

i agree that by deviding the total by 100 you would get the output you want, but the result will be wrong ^^

when you double the value "1" each of the 31 times the result IS 2147483648.00 and not 21474836.48....
maybe you just want to see an output of a double variable. in that case try to set pens to "1.1". then you´ll get a doublevalue and the output will be a correct result.

just another tip: you can let the iterator as integer. double is only needed in case you want to calculate whith floating point values which isn´t the case here.
runner7gk
New User
New User
 
Posts: 6
Joined: Sun Dec 18, 2011 7:42 am
Blog: View Blog (0)


Re: C programing help on a problem set

Post by Teh-brain-eater on Tue Dec 27, 2011 7:38 pm
([msg=63440]see Re: C programing help on a problem set[/msg])

ahh sweet thanks Logical. Runner7gk in harvards variation of pset pennies.c they have the decimal between the last 2 digits and I'm trying to copy that. They don't have the trailing zeros so I wanted it to be exactly the same but I appreciate your suggestions though.
Teh-brain-eater
New User
New User
 
Posts: 12
Joined: Thu Dec 22, 2011 12:01 pm
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests