I could use help with "for" structure

This is a discussion on I could use help with "for" structure within the C++ Programming forums, part of the General Programming Boards category; I am brand to to C++ and not well versed in programming at all. I have a lab assignment that ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    7

    I could use help with "for" structure

    I am brand to to C++ and not well versed in programming at all. I have a lab assignment that I would appreciate some help with. I do not expect you to do the assignment, but any suggestions of where to start would be great. The program should use "for" structure to find the smallest of several integers. Assume that the first value read specifies the number of values remaining and that the first number is Not one of the integers to compare. Thanks for any pointers, Ruger

  2. #2
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    try this

    Code:
    for (x=1;x<=SIZE;x++)
    {
        if(w>y)
        {
         //fill in remaining of code here.
        }
    }
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    you might want to start by reading the first number into a variable called i, which will be used in the for loop... then you create two variables to hold the data type (int/float)... preset one to 32000 or something so that you can be sure the first value will be written into it... then every time it loops, read in the new value and put it in one memory spot, and then compare it with the one you preset to 32000... if it's smaller, copy it into the box preset to 32000 and then do it all over again until the loop is over...

    I probably shouldn't have given you that much info, but try to write some code from that and if you still cant get it, just post your code here...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    7
    Thanks for the pointer so far, i'll let you all know if I get it to work or not!
    Last edited by ruger; 09-20-2003 at 11:31 PM.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    7
    This is what I have, but it doesn't find the smallest integer, it prints the last one entered for the smallest everytime! More suggestions please!
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
    	int num = 0;
    	int value;
    
    	cout << "please enter an integer to determine\n"
    		 << "the amount of integers I will compare\n"
    		 << "to find and display the smallest one: ";
    
    	cin >> num;
    
    	cout << "you may enter your integers now; ";
    	for ( int count = 1; count <= num; count++)
    		
    		{
    			cin >> value;
    		}
    
    		if ( value == value )
    
    			cout << value << " is the smallest integer" << endl;
    
    		return 0;
    }

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    1) you have to compare inside the loop
    2) you have to make sure it's lower, not equal

    --edit--
    pointing out some other stuff:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
    	int num = 0;
    	int value;
                    //what about the value to hold the current lowest int?
    
    	cout << "please enter an integer to determine\n"
    		 << "the amount of integers I will compare\n"
    		 << "to find and display the smallest one: ";
    
    	cin >> num;
    
    	cout << "you may enter your integers now; "; //what if they enter 4531863486816, meaning 4,531,8,6348,68,16
    	for ( int count = 1; count <= num; count++)
    		
    		{
    			cin >> value;
    		}
    
    		if ( value == value ) //change and put inside loop
    
    			cout << value << " is the smallest integer" << endl;
    
    		return 0;
    }
    Last edited by major_small; 09-21-2003 at 07:43 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    7
    Thanks for all the help, but I'm still stuck. I've been on this for another hour and can't get the new variable to initialize!
    And "not all control paths return a value"!

  8. #8
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Can I see the codes you're working on?
    How do you enter your input for the integers?

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    write it out in your native language first. Maybe something like this. If you can write it out, it is much easier to code.

    declare variable to hold the current smallest value.

    declare a variable to hold the number of values to be entered.

    obtain the number of values to be entered from user

    declare a loop counter to hold the current number of values entered.

    use a loop to obtain each value from user, incrementing counter variable by one each time through loop.

    in the loop:
    if this is the first value entered, then by definition it is the current smallest, so assign the entered value to current smallest varaible.

    else if this isn't the first value entered, then compare the current value with the current smallest value. if the current value is smaller than the current smallest value, assign the current value to the current smallest variable.

  10. #10
    Registered User
    Join Date
    Sep 2003
    Posts
    49

    try this....

    Newbie here......try this, it may help

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
    
    int smallest, times, count, num;
    count=1;
    num=0;
    smallest=0;
    
    
          cout << "Please enter how many Integers to compare ";
          cin >> times;
    
        for (; count <= times; ++count)
          {
          cout << count << " Enter your Integer ";
          cin >> num;
    
              if (num < smallest || count ==1){
                 smallest= num;
                 }
          }
          cout << "\n\n" << smallest << " is the smallest integer";
    
          system("PAUSE");
          return 0;
    }
    Last edited by SpEkTrE; 09-23-2003 at 11:53 AM.
    Code this

  11. #11
    Registered User
    Join Date
    Sep 2003
    Posts
    7
    This works but, I wonder if there is another way to code that wouldn't require a preset number to compare to, So no matter what numbers
    Code:
    #include <iostream>
    using std::cout;
    using std::cin; be
    using std::endl;
    
    int main()
    {
       int count;              
       int value;  
       int smallest = 999999999999;
    
       cout << "Enter the total number of integers to read: ";
       cin >> count;
    
     
       cout << "Enter " << count << " integers to compare: ";
    
    
       for (int i = 0; i < count; i++)
       {
         
          cin >> value;
    
      
          if (value < smallest) {
             smallest = value;
          }
       }
    
       
       cout << "\nThe smallest value found in this list is: "
            << smallest
            << endl;
    
       return 0;
    }
    were entered it would compare them? [/code]

  12. #12
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    So no matter what numbers were entered it would compare them?
    Your code already does that. Or are you saying, you dont want to have to set smallest to 999999999999?

    Code:
    #include <iostream>
    using std::cout;
    using std::cin; be
    using std::endl;
    
    int main()
    {
       int count;              
       int value;  
       int smallest;
    
       cout << "Enter the total number of integers to read: ";
       cin >> count;
    
     
       cout << "Enter " << count << " integers to compare: ";
    
       cin >> smallest; //take the first integer and put it straight into smallest
       for (int i = 1; i < count; i++) //note: initial i changed to 1
       {
         
          cin >> value;
    
      
          if (value < smallest) {
             smallest = value;
          }
       }
    
       
       cout << "\nThe smallest value found in this list is: "
            << smallest
            << endl;
    
       return 0;
    }
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  13. #13
    Registered User
    Join Date
    Sep 2003
    Posts
    7
    Thanks for all the help, I've got it!!

  14. #14
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    So what was the problem?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  15. #15
    Registered User
    Join Date
    Sep 2003
    Posts
    7
    No, problems just operator error!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. repetition with the "for" structure
    By Lillian in forum C Programming
    Replies: 3
    Last Post: 10-19-2002, 09:28 PM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 04:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21