gaza165 Posted February 17, 2009 Share Posted February 17, 2009 what i am trying to do, is accept numbers from users and store them into an array. as soon as a user enters a negative number i want the program to break and calculate the average of those numbers and display it. as you can see, i have an if function inside my for loop to check if the number is below 0 then to break. am i doing this right or is my logic messy? should i have used a while loop or a do while!! for(i=0; ; i++) { printf("%2d> ",i+1); scanf("%d", &numbers[i]); if(numbers[i] < 0) { break; } sum += numbers[i]; count = i + 1; } Link to comment https://forums.phpfreaks.com/topic/145553-need-help-with-programming-logic/ Share on other sites More sharing options...
cola Posted February 17, 2009 Share Posted February 17, 2009 Try this ?php $ia[}=$i: foreach ($ia as $i) { if (numbers[$i]>0) { sum += numbers[i]; count =$i + 1; } } ?> Link to comment https://forums.phpfreaks.com/topic/145553-need-help-with-programming-logic/#findComment-764133 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.