# Help finding largest number in loop

Page 1 of 1

## 1 Replies - 735 Views - Last Post: 10 February 2014 - 01:43 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=339733&amp;s=f3c3a6e5c13853fec138ce57e03d9404&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 sunvsrhinovirus

• New D.I.C Head

Reputation: 1
• Posts: 15
• Joined: 10-February 14

# Help finding largest number in loop

Posted 10 February 2014 - 01:26 PM

This is for homework and I've tried about everything I just can't figure it out. I need to calculate the expected time it takes to do an activity using a formula provided and after the loop is broken it needs to show the number of projects processed and the project with the longest expected time. I've got everything working except for finding the the project with the longest expected time. What I have here just keeps displaying the number of the last project processed regardless of if it was the largest or not, specifically lines 54-61 is what i can't seem to figure out.

```#include <stdio.h>

int main( void )
{
unsigned int counter;
int projectnumber;
int optimistictime;
int realistictime;
int pessimistictime;
int longesttime;
int longestproject;

float expectedtime;

counter = 0;
expectedtime = 0;

//getfirstproject
printf( "%s", "Would you like to calculate the expected time for an Activity?\n1 for yes, -1 for no: ");
longestproject = 0;
longesttime = 0;

while ( ask > 0 ) {

printf( "%s", "Enter Project Number: " );
scanf( "%d", &projectnumber);

printf( "%s", "Enter Optimistic Time(in weeks): " );
scanf( "%d", &optimistictime);

printf( "%s", "Enter Realistic Time(in weeks): " );
scanf( "%d", &realistictime);

printf( "%s", "Enter Pessimistic Time(in weeks): " );
scanf( "%d", &pessimistictime);

counter = counter + 1; // increment counter
//calculate and display information
printf("The Project Number is %d\n", projectnumber);
printf("The Optimistic Time is %d\n", optimistictime);
printf("The Realistic Time is %d\n", realistictime);
printf("The Pessimistic Time is %d\n", pessimistictime);

expectedtime = (float) (optimistictime + 4*realistictime + pessimistictime)/6;

printf("The Expected Time is %.1f\n", expectedtime);

if (expectedtime > longesttime)
{
expectedtime = longesttime;
longestproject = projectnumber;
}
else {

}

printf( "%s", "Would you like to calculate the expected time for an Activity?\n1 for yes, -1 for no: ");

} // end while

while (ask < 0 ) {

printf("The Number of Records Processed is: %d\n", counter );
printf("The Project Number with the Longest Expected time is: %d\n", longestproject);
system("PAUSE");
return 0;

}

}

```

Is This A Good Question/Topic? 0

## Replies To: Help finding largest number in loop

### #2 sunvsrhinovirus

• New D.I.C Head

Reputation: 1
• Posts: 15
• Joined: 10-February 14

## Re: Help finding largest number in loop

Posted 10 February 2014 - 01:43 PM

Line 56 was the problem, I needed to switch it around to longesttime = expectedtime;