how could i possibly write an infinite loop that have to be terminated when the number = 0 (or below)?... & also have the program allow multiple number entries to find the highest # out of the bunch... grrr, this prog of chap 6 exercises def sux!!!
how could i possibly write an infinite loop that have to be terminated when the number = 0 (or below)?... & also have the program allow multiple number entries to find the highest # out of the bunch... grrr, this prog of chap 6 exercises def sux!!!
simple.Code:while (1) { if (number <= 0) break; }
i don't know what you mean for the other one.
create an array of integers and have inputs stored in the array. Write a function that steps through the integer array looking for the highest number.& also have the program allow multiple number entries to find the highest # out of the bunch..
if it's an infinite loop then it can't be terminated now can it!
this is what i have *so* far~
/* SEC6.1 EX 1 */
/* DATE: 07-25-02 */
#include <stdio.h>
int main()
{
float n, highest;
printf("Enter a number: ");
scanf("%f", &n);
while (n > 0) {
printf("Enter a number: ");
scanf("%f", &n);
}
if ( n<= 0)
printf("The highest number of the entries was %f", highest);
printf("\n");
return 0;
}
The last if statement is not needed.Code:printf("Enter a number: "); scanf("%f", &n); while (n > 0) { printf("Enter a number: "); scanf("%f", &n); } if ( n<= 0) printf("The highest number of the entries was %f", highest);
You're missing a step and also a do/while loop would better fit:
highest = 0;
do
{
printf("Enter a number: ");
scanf("%f", &n);
if(n > highest)
highest = n;
}while(n > 0);
awwww, thanx Cshot (that did the trick [do]~!!! ... now i have another Q... this program can have either integer or non-integer values... i can't use either %d or %f... what should i use?~
thanx a bunch everyone~!!!!!!!!!!!
Why can't you use %f? Do you mean you're supposed to check for invalid inputs also such as characters?
this is what the exercise in the book says:
1. Write a program that finds the largest in a series of numbers entered by the user. The program must prompt the user to enter numbers one by one. When the user enters 0 or a negative number, the program must display the largest nonnegative number entered:
Enter a number: 60
Enter a number: 38.3
Enter a number: 4.89
Enter a number: 100.62
Enter a number: 75.2295
Enter a number: 0
The largest number entered was 100.62
***NOTICE that the numbers aren't necessarily integers***
here's my code so far (I need to fix the part about the entries not necessarily being integers [but not floating 5000decimal points~
/* SEC6.1 EX 1 */
/* DATE: 07-25-02 */
#include <stdio.h>
int main()
{
float n, highest;
highest = 0;
printf("Enter a number: ");
scanf("%f", &n);
do
{
printf("Enter a number: ");
scanf("%f", &n);
if(n > highest)
highest = n;
}while(n > 0);
return 0;
}
Well, if you want to be able to enter both integers and decimals, just keep it a float.
This is my signature. Remind me to change it.
Note that you do not need the first printf/scanf statements. The do/while loop takes care of it. Before you were using a while loop so the statements were needed.Code:printf("Enter a number: "); scanf("%f", &n); do { printf("Enter a number: "); scanf("%f", &n); if(n > highest) highest = n; }while(n > 0);