1 Replies - 226 Views - Last Post: 10 February 2014 - 01:43 PM Rate Topic: -----

#1 sunvsrhinovirus  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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;
	int ask;

	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: ");
	scanf( "%d", &ask); 
	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: ");
	scanf( "%d", &ask);

	
       
   } // 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  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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;
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1