Okay, below I will copy my problem from the book, and my "pseudo code" then I had another idea on how to write the code, so I have a second "pseudo code" example. Are either on the right track? Which should I further develop if any? First time writing a loop, so wasn't really sure where to start.
Code:
//I'm trying to think about how to begin writng this program, so I want to put my
//ideas on paper, (like pseudo code, because I don't really know how to do that-
//but this is my first attempt at some kind of pseudo code)
//Since this chapter taught loops, while, do and for, I'm sure this exercise will need
//a loop, but how do I know which one works best?
//pseudo code for exercise 1 chapter 6
//exercise........
//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. The numbers musn't necessarily
//be integers.
//Is the below anywhere close? I'm curious to know how the user can continue to store
//new numbers in &user_number, wouldn't each new number entered overwrite the previously
//entered number? Or will the numbers somehow be kept seperated in user_number? I'm thinking
//that each new user entry of a number needs to be user_number1, user_number2, etc. etc.
//But since this is a loop, it's going to keep using the same printf, scanf.
// Known problem... I have no idea how to tell the computer to print the largest number of
// user input. When it comes time for the printf to print the largest number, how
// do I distiguish the largest number from just any number? I thought about telling
// the computer to print the number with the most character spaces used, but that
// won't work as .0000001 is obviously not bigger than 1. Hmmmm...?
//I hope somebody will give me good news!!
float user_number ;
for ( ; user_number > 0 ; ) { // for and expression
printf (enter a number:) ; // loop body
scanf (accepts user number %f, &user_number) //loop body
if (user number <= 0) break ; // loop body with breakout
}
printf ("The largest nonnegative number you entered is: %f, user_number) ;
return 0 ;
}
//Or my second idea just kicked in.......
//I need the printf and scanf to be in a loop that repeats until 0 or a negative
//number is entered, so I need to run the loop before the variable is tested. So
//maybe I need a "do" loop instead? Do (statement) While (expression) statement
// I'm sure there's something wrong here too, because I don't yet know how, but am I
//on the right track? I don't feel like my while is in the loop? Do I need to extend
//the brackets and include while? If so, where do I break out?
do {
printf (Enter your number) ;
scanf (&f, user_number) ;
}
while (user_number > 0) ;
if (user_number <= 0)
printf(The largest number you entered was) ;
return 0 ;