Problem with a simple program

• 06-15-2006
Salgat
Problem with a simple program
Alright, I just started teaching myself how to program C a couple days ago, so don't make fun of me too much!

Code:

```#include <stdio.h> //purpous is to have 4 numbers input and the average displayed main() {         int a = 1;  //counter         double b;  //numbers to be input         double c = 0.0;  //total of all b         char d;  //suffix to number         while(a < 5)          {                 printf("Input your %d%c number: ",a,d==1?'st':(a==2?'nd':(a==3?'rd':'th')));  //input numbers                 scanf("%fl",&b);  //input numbers to be averaged                 c = c + b;  //add all the numbers up                 ++a;  //tell program to go to next number                 if (a<5) continue;        //if not done inputting numbers, loops                        printf("The average of the 4 numbers is %fl.",c/4);  //displays average of the sum of all 4 numbers inputted         }             return(0);         }```
My problem is the program goes goofy when displaying the final averaged number. Does it have to do with the "Double"? Im getting a result a 0.0000001. Also it displays "t" at the end of each number when displaying which number to input.
• 06-15-2006
quzah
Since when are 'st', 'nd', 'rd' and 'th' single characters?
= is an assignment.
== is an equality test.

Quzah.
• 06-15-2006
Salgat
I know about = and ==, not sure about how that applies here, but thanks about the char note. Also, any reason why its not reading(or outputting?) the correct double variables? Thats my main problem.
• 06-15-2006
Maragato
avarage.c:14:57: warning: multi-character character constant
avarage.c:14:67: warning: multi-character character constant
avarage.c:14:77: warning: multi-character character constant
avarage.c:14:82: warning: multi-character character constant
avarage.c: In function `main':
avarage.c:14: warning: overflow in implicit constant conversion
avarage.c:15: warning: float format, double arg (arg 2)
avarage.c:24:2: warning: no newline at end of file

I can't grok your 14 line what a heck do you want to do?
• 06-15-2006
Salgat
If you talking about the first output, Im trying to have it go 1st, then next time around 2nd, then next time 3rd, etc. To be honost, Im not 100% sure you can put the trinary ?: inside another one. But like I said, my main focus is the output being incorrect for the average value.
• 06-15-2006
quzah
Quote:

Originally Posted by Salgat
I know about = and ==, not sure about how that applies here

Code:

`printf("Input your %d%c number: ",a,d=1?'st':(a=2?'nd':(a=3?'rd':'th')));  //input numbers`

Quzah.
• 06-15-2006
Salgat
Oops, hehe thanks Quzah, hoorah! But about the double variable, anyone know?
• 06-15-2006
Maragato
Quote:

Originally Posted by Salgat
If you talking about the first output, Im trying to have it go 1st, then next time around 2nd, then next time 3rd, etc. To be honost, Im not 100% sure you can put the trinary ?: inside another one. But like I said, my main focus is the output being incorrect for the average value.

For gods sake take a book like K&R you are guessing the language that is never gonna work I will give you a basic hint of what you need.

Code:

```#include <stdio.h> #include <stdlib.h> int main(void){ · double a,b,c,avarage; · printf("Give me 3 numbers separated by spaces: "); · scanf("%lf %lf %lf",&a,&b,&c); · avarage = (a+b+c)/3; · printf("The avarage is %.2lf\n",avarage); · return 0; }```
• 06-15-2006
Salgat
Thanks! I was trying to apply a few things I was learning and reduce the amount of variables used, unfortunately I got a few things wrong. I know I may not be doing it all perfectly to the book, and I know C is a percise language, but was expirementing with some new operands and although that didn't work, I'm still wondering why its not outputting correctly.

Heres a revision for debugging purpouses.

Code:

```#include <stdio.h> //purpous is to have 4 numbers input and the average displayed int main(void) {         double b;  //numbers to be input         double c = 0.0;  //total of all b         int a = 1;  //counter         while(a < 5)          {                 printf("\nInput your %d number: ",a);  //input numbers                 scanf("%fl",&b);  //input numbers to be averaged                 printf("The number you input is: %fl",b);  //show your input                 c = c + b;  //add all the numbers up                 ++a;  //tell program to go to next number                 if (a<5) continue;        //if not done inputting numbers, loops                        printf("The average of the 4 numbers is %.2fl.",c/4);  //displays average of the sum of all 4 numbers inputted         }             return(0);         }```

I just want to know, why isnt it reading my double variable "b" correctly.
• 06-15-2006
Dave_Sinkula
Code:

`scanf("%fl",&b);  //input numbers to be averaged`
Try spelling %lf correctly. And for printf, just use %f.
• 06-15-2006
Salgat
Oh ya! Haha, thanks :D (I would use floats, and a more simple setup, but like I said, trying out different things);

(ended up finishing it, thanks guys :) )

Code:

```#include <stdio.h> //Title: Min, Max, and Average //Purpose: Is to have a given amount of numbers input and the average displayed //Creator: Austin Salgat main() {     int a = 1;  //counter           int d;  //amount of #s averaged         double b;  //current # inputted         double c = 0.0;  //total of all b         double e = 0.0;  //max #         double f = 999.999;  //min #         printf("**************************************\n***  Find Min, Max, and Average  ***\n***    Created by Austin Salgat    ***\n**************************************");         printf("\n\nHow many numbers do you wish to put into this average?: ");     scanf("%d",&d);  //input how many numbers to average         while(a <= d)          {               printf("\nInput number %d: ",a);  //input numbers                 scanf("%lf",&b);  //input numbers to be averaged         e = (b>e) ? b : e;  //input new max                 f = (b<f) ? b : f;  //input new min         c = c + b;  //add all the numbers up                         printf("\nThe average of the %d number%c is %.2lf.",a,a==1?' ':'s',c/a);  //displays average of the sum of all 4 numbers inputted                 printf("\nThe max is %.2lf and the min is %.2lf.\n",e,f);  //displays min and max                 ++a;  //tell program to go to next number         }         printf("\n\n\nTo exit this program, simply type a letter and press enter!: ");     scanf("%d",a);         return(0);         }```