Thread: Here's another prgram I need help on!

  1. #1
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99

    Here's another prgram I need help on!

    Write a program that finds the smallest of several integers. Assume that the first value read specifies the number of values remaining.


    I have most of it but im missing some stuff. I know its a loop.


    Code:
    #include <stdio.h>
    
    int main () {
    
    	int integer;
    	int min;
    	int counter;
    
    	prinf("Enter integer");
    	scanf("%d", &integer);
    
    
    	
    	min = 9999;
    	integer = 0;
    	
    	if(integer < min){ 
    		min = integer;
    
    }
    
    	printf("The smallest integer is %d", min);
    
    	return 0;
    }

  2. #2
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    I suggest setting min to the first integer to be compared instead of 9999, that way it will work if your smallest value is > 9999

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by jturner38 View Post
    Write a program that finds the smallest of several integers. Assume that the first value read specifies the number of values remaining.


    I have most of it but im missing some stuff. I know its a loop.


    Code:
    #include <stdio.h>
    
    int main () {
    
    	int num;  //please don't call an int, "integer" :)
    	int min;
    	int counter;
    
    	prinf("Enter how many numbers you wish to enter:  ");
    	scanf("%d", &counter);
            //getchar();
    
    	for(i = 0; i < counter; i++)  {               //Edit: counter, not num, of course. Thanks
               printf("Enter a number, please: ");
               scanf(" %d", &num);
               //getchar();           
               if(i == 0)
                  min = num;
               else
                  if(num < min)
                     min = num;
           }
    
    	printf("The smallest integer is %d", min);
            printf("\n\n Press enter when ready\n");
            getchar();   //holds the console window open
           	return 0;
    }
    If you see the program skipping over the scanf() calls, remove the slashes before the getchar() lines of code.
    Last edited by Adak; 03-04-2009 at 08:25 AM.

  4. #4
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by Adak View Post
    If you see the program skipping over the scanf() calls, remove the slashes before the getchar() lines of code.
    I think you meant

    Code:
    for(i=0; i < counter; i++)
    not

    Code:
    for(i=0; i < num; i++)

  5. #5
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Reply to his updated code in the other thread:
    Code:
    	counter = 0;
    	min = ;
    	counter++;
    what? why not just define counter = 1
    Code:
    if(integer > counter){ 
    		min = min;
    what?
    Code:
    else(integer == counter);
    you don't use argument lists after an else statement, you would need an else if statement

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making your prgram repeat
    By chibby2 in forum C++ Programming
    Replies: 1
    Last Post: 07-03-2005, 11:14 AM