A little C help please

ManyBeers

Platinum Member
Aug 30, 2004
2,519
1
81
Code:
#include <stdio.h>

float la, pay;



int main(void)



   {

      printf("Enter loan amount: ");

      scanf ("%f", &la);

      printf("Enter payment: ");

      scanf ("%f", &pay);

      printf("%.2f\n%.2f\n", la, pay);

int i=0;

        while (la>0 )

           {  la=la-pay;

          printf("%.2f\n", la);

           i--;

         }      

return 0;

   }
That,s my code. if you run it:
Enter loan amount: 20000
Enter payment: 200
20000.00
200.00
19800.00
19600.00
19400.00
19200.00
19000.00
18800.00
18600.00
18400.00
18200.00
18000.00
17800.00
17600.00
17400.00
17200.00
17000.00
16800.00
16600.00
16400.00
16200.00
16000.00
15800.00
15600.00
15400.00
15200.00
15000.00
14800.00
14600.00
14400.00
14200.00
14000.00
13800.00
13600.00
13400.00
13200.00
13000.00
12800.00
12600.00
12400.00
12200.00
12000.00
11800.00
11600.00
11400.00
11200.00
11000.00
10800.00
10600.00
10400.00
10200.00
10000.00
9800.00
9600.00
9400.00
9200.00
9000.00
8800.00
8600.00
8400.00
8200.00
8000.00
7800.00
7600.00
7400.00
7200.00
7000.00
6800.00
6600.00
6400.00
6200.00
6000.00
5800.00
5600.00
5400.00
5200.00
5000.00
4800.00
4600.00
4400.00
4200.00
4000.00
3800.00
3600.00
3400.00
3200.00
3000.00
2800.00
2600.00
2400.00
2200.00
2000.00
1800.00
1600.00
1400.00
1200.00
1000.00
800.00
600.00
400.00
200.00
0.00
it does this
Would somebody show me how nto order it into columns or rows of 12.
Thanks

ps I'm trying to teach myself C and it isn't easy.
 
Last edited:

mxnerd

Diamond Member
Jul 6, 2007
6,799
1,103
126
C:
while (la > 0)
{
  la = la - pay;
  printf("%8.2f  ", la);
  i--;
  if (i % 12 == 0) {
    printf("\n");
  }
}
 

ManyBeers

Platinum Member
Aug 30, 2004
2,519
1
81
C:
while (la > 0)
{
  la = la - pay;
  printf("%8.2f  ", la);
  i--;
  if (i % 12 == 0) {
    printf("\n");
  }
}
Thanks for reply twill. Try it later today and post tomorrow. My internet access is tough right now because McDonald's is closed. Haha. I don't have my own internet right now and am posting this from a nearby Lowe's.
 

mv2devnull

Golden Member
Apr 13, 2010
1,519
154
106
The main point is that this one does two things that should be separate:
Code:
printf( "%f\n", la );
1. It prints a value
2. It prints a newline

If you want one newline per 12 values, then you must not print newline after every value.

The code by mxnerd prints every value and keeps a counter that allows conditional print of newline on every 12th iteration.
Overall, issue is not in C but in logic.
 

ManyBeers

Platinum Member
Aug 30, 2004
2,519
1
81
The main point is that this one does two things that should be separate:
Code:
printf( "%f\n", la );
1. It prints a value
2. It prints a newline

If you want one newline per 12 values, then you must not print newline after every value.

The code by mxnerd prints every value and keeps a counter that allows conditional print of newline on every 12th iteration.
Overall, issue is not in C but in logic.

hey i'm learning. i printed a newline after every iteration because it was easier to read than having to scroll horizontally to read it.

if (i % 12 == 0) {
printf("\n");
What exactly is that code saying. I don't understand the (i % 12==0) part. I'snt that the modulus(remainder operator)? shouldn't 12 be on the left side?
like this
Code:
dec%(8*8*8*8*8*8*8*8*8)/(8*8*8*8*8*8*8*8
that's fom a decimal octal converter and the dec(decimal) number is the bigger value.

By the way that code snippet from my dec/oct converter is the only way i could figure out how to do it convert dec to oct though i now have found far more succinct examples.
 

serpretetsky

Senior member
Jan 7, 2012
642
26
101
if (i % 12 == 0) {
printf("\n");
What exactly is that code saying. I don't understand the (i % 12==0) part. I'snt that the
i/12 means give me the whole integer of i divided by 12.
i%12 means give me the remaining integer of i divided by 12.

Example:
15/12 = 1
15%12 = 3

Simple code to show how num%5 relates to increasing num:
Code:
#include <stdio.h>

int main()
{
    int num;
    for(num=0; num<13; ++num){
        printf("num: %i num%%5: %i \n",num, num%5);
    }

    return 0;
}

result:
Code:
num: 0 num%5: 0                                                                                                                 
num: 1 num%5: 1                                                                                                                 
num: 2 num%5: 2                                                                                                                 
num: 3 num%5: 3                                                                                                                 
num: 4 num%5: 4                                                                                                                 
num: 5 num%5: 0                                                                                                                 
num: 6 num%5: 1                                                                                                                 
num: 7 num%5: 2                                                                                                                 
num: 8 num%5: 3                                                                                                                 
num: 9 num%5: 4                                                                                                                 
num: 10 num%5: 0                                                                                                                
num: 11 num%5: 1                                                                                                                
num: 12 num%5: 2
 

ManyBeers

Platinum Member
Aug 30, 2004
2,519
1
81
Try printf("%12.2f\n", la), does that work for you?
I tried this and all it does is format the look of how the the values print
like this:
19800.00
to this:
19800.00
it doesn't separate the values at any point. mxnerd's whileloop did that.
your code made 12 characters wide and 2 characters after the decimal point.
if you place a 0 between the % and the 12 it will llok like this:
000019800.00
 

serpretetsky

Senior member
Jan 7, 2012
642
26
101
I tried this and all it does is format the look of how the the values print
like this:
19800.00
to this:
19800.00
it doesn't separate the values at any point. mxnerd's whileloop did that.
your code made 12 characters wide and 2 characters after the decimal point.
if you place a 0 between the % and the 12 it will llok like this:
000019800.00
Yes, that is correct. It was not clear to me what you wanted from the original post. mxnerd's is probably what you want.
 

ManyBeers

Platinum Member
Aug 30, 2004
2,519
1
81
i/12 means give me the whole integer of i divided by 12.
i%12 means give me the remaining integer of i divided by 12.

Example:
15/12 = 1
15%12 = 3

Simple code to show how num%5 relates to increasing num:
Code:
#include <stdio.h>

int main()
{
    int num;
    for(num=0; num<13; ++num){
        printf("num: %i num%%5: %i \n",num, num%5);
    }

    return 0;
}

result:
Code:
num: 0 num%5: 0                                                                                                                
num: 1 num%5: 1                                                                                                                
num: 2 num%5: 2                                                                                                                
num: 3 num%5: 3                                                                                                                
num: 4 num%5: 4                                                                                                                
num: 5 num%5: 0                                                                                                                
num: 6 num%5: 1                                                                                                                
num: 7 num%5: 2                                                                                                                
num: 8 num%5: 3                                                                                                                
num: 9 num%5: 4                                                                                                                
num: 10 num%5: 0                                                                                                               
num: 11 num%5: 1                                                                                                               
num: 12 num%5: 2
0k thanks. I will play around with this when I get home.
 

mxnerd

Diamond Member
Jul 6, 2007
6,799
1,103
126
I haven't programmed for a while. C language is hard for many people.

It's hard because it uses pointers, and you have to deal with memory management.

It's very easy to go wrong with these two thing.

For modern computing, I would urge OP to learn GO Language by Google. It's easy and no memory management to worry about.



Some people mentioned C is better because it forces you to learn low level programming that's close to the machine, but in modern days and in most cases you don't have that necessity. GO also has native web support. https://golang.org/pkg/net/http/

Youtube is a very good source for tutorials.
 
Last edited:

purbeast0

No Lifer
Sep 13, 2001
53,475
6,316
126
You should learn concepts before going any further. I mean this is like day 1 stuff you are having trouble with and just asking for people to solve it for you. If you can't figure out these simple issues on your own, then you are going nowhere with programming. I'm not trying to be a dick, just being blunt and honest.

He's nowhere CLOSE to dealing with pointers so even bringing them up at this point is kind of moot.
 
Reactions: mxnerd

serpretetsky

Senior member
Jan 7, 2012
642
26
101
Meh, it never hurts to ask questions about things, whether you are a beginner or expert. Ofcourse studying the concepts yourself and trying to figure it out is also very important. That's all I will say on that topic. OP, if you have any other questions feel free to ask.
 

purbeast0

No Lifer
Sep 13, 2001
53,475
6,316
126
Meh, it never hurts to ask questions about things, whether you are a beginner or expert. Ofcourse studying the concepts yourself and trying to figure it out is also very important. That's all I will say on that topic. OP, if you have any other questions feel free to ask.
I don't disagree with anything you said, but when you are asking questions about the mod operator and the correct syntax, if you can't figure that out yourself with the power of the internet, then you are in for a rude awakening.

It also appeared that he asked for help, got someone to give him code, he tried the code and it didn't do what he expected, so he asked why.

Again, not trying to be a dick, but things like this are what you need to go and figure out yourself so you can learn. Having things spoon fed to you in this field is a terrible idea. I worked with someone who was a copy/paste guy and didn't really understand why things are working, and let me just tell you, it made for some bad code that had to be reworked big time.

It's better to understand earlier on that being a good programmer requires a certain mindset. Hell I know I wish I realized this before I was like 6-7 years into the industry. Although I'm in a good spot now so I can't complain. But my first job out of school, I definitely cruised by way too easily not really improving my skills much, and my salary reflected that.
 

mxnerd

Diamond Member
Jul 6, 2007
6,799
1,103
126
Best way to learn a language is take a book, read from 1st page to last. Do every excises the author asks you to do and solve it by yourself.

Start with a book that's not too thick though, don't get you overwhelmed. It's a progressive process. You will know very soon whether you really want to get into the field.
 
Last edited:

ManyBeers

Platinum Member
Aug 30, 2004
2,519
1
81
I don't disagree with anything you said, but when you are asking questions about the mod operator and the correct syntax, if you can't figure that out yourself with the power of the internet, then you are in for a rude awakening.

It also appeared that he asked for help, got someone to give him code, he tried the code and it didn't do what he expected, so he asked why.

Again, not trying to be a dick, but things like this are what you need to go and figure out yourself so you can learn. Having things spoon fed to you in this field is a terrible idea. I worked with someone who was a copy/paste guy and didn't really understand why things are working, and let me just tell you, it made for some bad code that had to be reworked big time.

It's better to understand earlier on that being a good programmer requires a certain mindset. Hell I know I wish I realized this before I was like 6-7 years into the industry. Although I'm in a good spot now so I can't complain. But my first job out of school, I definitely cruised by way too easily not really improving my skills much, and my salary reflected that.
I only have internet when i drive to a nearby Lowes which I did today and am posting from my truck. By posting in this forum I nam using the internet to get help. nobody is spoon feeding me i try to understand every piece of code I write. I like to know what i'm writing
Code:
/*decimal_octal_converter*/

#include <stdio.h>
/*---------------*/

int dec, oct;

  int main(void)

    {
     printf("Enter a number between 0 and 1,000,000,000: ");
     scanf ("%d", &dec);


     printf("Octal value: %d%d%d%d%d%d%d%d%d%d", dec/(8*8*8*8*8*8*8*8*8), dec%(8*8*8*8*8*8*8*8*8)/(8*8*8*8*8*8*8*8), dec%(8*8*8*8*8*8*8*8)/(8*8*8*8*8*8*8), dec%(8*8*8*8*8*8*8)/(8*8*8*8*8*8), dec%(8*8*8*8*8*8)/(8*8*8*8*8), dec%(8*8*8*8*8)/(8*8*8*8), dec%(8*8*8*8)/(8*8*8), dec%(8*8*8)/(8*8), dec%(8*8*8)%(8*8)/(8), dec%8);

     return 0;
    }
That's my code. It's unwieldy a lot of typing even ridiculous but it works up to i think an signed 32 bit integer. It's the only way I could figure out how to do it This was an exercise in a C programming pdf' KNKing volume 2 or something. Anyways I have subsequently found dec/0ct converter code that uses a modulus loop.
Code:
#include <stdio.h>
#include <math.h>

int convertDecimalToOctal(int decimalNumber);
int main()
{
    int decimalNumber;

    printf("Enter a decimal number: ");
    scanf("%d", &decimalNumber);

    printf("%d in decimal = %d in octal", decimalNumber, convertDecimalToOctal(decimalNumber));

    return 0;
}

int convertDecimalToOctal(int decimalNumber)
{
    int octalNumber = 0, i = 1;

    while (decimalNumber != 0)
    {
        octalNumber += (decimalNumber % 8) * i;
        decimalNumber /= 8;
        i *= 10;
    }

    return octalNumber;
}

but this code will be correct only up decimal number 295,600,127.

C:\Users\ManyBeers\Documents\Programming\Mystuff>decoct.exe
Enter a decimal number: 295600127
295600127 in decimal = 2147477777 in octal
C:\Users\ManyBeers\Documents\Programming\Mystuff>decoct.exe
Enter a decimal number: 295600128
295600128 in decimal = -2147467296 in octal


Why? what is the limiting factor in that code?

I'm 67 so I probably am not going into the field but you never know.
AAnd that code of mine gets the second digit wrong but my other computer
will print 17777777777 if you enter 2147483647(because I changed the code slightly.
Anyways i just want to clear up a misconception that I copy/paste.
I like to know what i'm doing.
Thanks









 

serpretetsky

Senior member
Jan 7, 2012
642
26
101
Why? what is the limiting factor in that code?
Well, first of all, you can print a number in octal by using %o instead of %d:

printf("octal representation: %o\n", numberHere);

second of all, with that code:
octalNumber overflows into a negative number at that point. See two's complement if you want understand how most computers represent negative numbers internally.

Basically 0x7FFFFFFF (thats hex) is the largest number you can represent with a 32-bit signed int. The next number after that, (int)0x80000000, represents -0x80000000. (int)0x80000001 represents -0x7FFFFFFF.

You can lift this limit to be much higher if instead of int you redeclare all of the variables as "long long". "long long" is a 64bit signed integer. You would also need change printf. %d expects an "int". %lld expects a "long long".
 

ManyBeers

Platinum Member
Aug 30, 2004
2,519
1
81
Well, first of all, you can print a number in octal by using %o instead of %d:

printf("octal representation: %o\n", numberHere);

second of all, with that code:
octalNumber overflows into a negative number at that point. See two's complement if you want understand how most computers represent negative numbers internally.

Basically 0x7FFFFFFF (thats hex) is the largest number you can represent with a 32-bit signed int. The next number after that, (int)0x80000000, represents -0x80000000. (int)0x80000001 represents -0x7FFFFFFF.

You can lift this limit to be much higher if instead of int you redeclare all of the variables as "long long". "long long" is a 64bit signed integer. You would also need change printf. %d expects an "int". %lld expects a "long long".

Thanks. This is probably a bit over my head right now. I'll be back with more questions though.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |