Results 1 to 10 of 10

Thread: [NEED HELP] C++ Looping

  1. #1
    Join Date
    Dec 2011
    Location
    -bash
    Posts
    515
    Mentioned
    0 Post(s)
    Quoted
    27 Post(s)

    Default [NEED HELP] C++ Looping

    Code is below
    Problem: Whenever I click compile, it gives an error. Now I have kinda life cpp and moved on to pascal so my cpp might be a bit rusty but someone please help with this.

    Don't worry about the logic I used in the coding, I just want to know if the format is right. If you just can't stand the logic then go ahead and suggest something.

    Code:
    #include <iostream>
    using namespace std;
    int smallest(int array[], int size)
    {
    	int a;
    	if (size ==1)
    		{
    			return array[0];
    		}
    	else
    		{
    			for (int i = 0; i < size; i++)
    				{
    					If (array[i] < array[i+1])
    					      {
    								a = array[i];
    					      }
    					else
    					{
    						a = array[i+1];
    					}
    				}
    					If (i = 10) then
    						{
    							return a;
    						}
    		}
    
    
    
    
    }
    
    int main()
    {
    	int array[10]={-1, 5, -23, 90, 4, 5, 6, -2, -100,9 };
    	cout<<smallest(array, 10);
    	return 0;
    }
    Last edited by Recursive; 06-03-2012 at 04:11 AM.

  2. #2
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    I don't have a compiler on me atm.. I'm actually installing one but try this:

    C++ Code:
    int Smallest(int Array[], int size)
    {
        if (size == 1)
            return Array[0];

        int A = Array[0];
        for (int I = 0; I < size; I++)
            A = Array[I] < A ? Array[I] : A;

        return A;
    }
    Last edited by Brandon; 06-03-2012 at 06:13 AM.
    I am Ggzz..
    Hackintosher

  3. #3
    Join Date
    Jan 2011
    Posts
    151
    Mentioned
    0 Post(s)
    Quoted
    8 Post(s)

    Default

    #include <iostream>
    using namespace std;
    int smallest(int array[], int size)
    {
    int a = 0;
    if (size ==1)
    {
    return array[0];
    }
    else
    {
    for (int i = 0; i < size; i++)
    {
    if (array[i] < array[i+1]) // 'if' needs to be lowercase. also, value is messed up
    // because it traverses off the end of the array with (i+1)
    {
    a = array[i];
    }
    else
    {
    a = array[i+1];
    }
    }
    /*
    if (i = 10) then // this isn't working because it is outisde of the loop,
    // but you don't need it.
    {
    return a;
    }
    */
    return a; // just return a when the loop finishes, you don't need an if statement
    }




    }

    int main()
    {
    int array[10]={-1, 5, -23, 90, 4, 5, 6, -2, -100, 9};
    cout<<smallest(array, 10);
    system("pause");
    return 0;
    }
    I fixed the compiling errors, if you are trying to find the lowest number in the array, you need to compare smallest[i] to 'a' every time, not compare it to [i+1].

  4. #4
    Join Date
    Dec 2011
    Location
    -bash
    Posts
    515
    Mentioned
    0 Post(s)
    Quoted
    27 Post(s)

    Default

    stupid this is that even when I remove the -100 from the list of array, it still shows -100 no matter what else I tell it to display

    E: How do I select everything and comment them all at once? I used to know how, but I forgot

    E2: When I run this I get back the value [-100]

    Code:
    #include <iostream>
    using namespace std;
    int smallest(int array[], int size)
    {
    int a = 0;
    if (size ==1)
    	{
    		return array[0];
    	}
    else
    	{
    		for (int i = 0; 1 < size-1; i++)
    		{
    			if (array[i] < array[i+1]) // 'if' needs to be lowercase. also, value is messed up
    					// because it traverses off the end of the array with (i+1)
    		{
    				a = array[i];
    		}
    			else
    			{
    				a = array[i+1];
    			}
    		}
    		/*
    if (i = 10) then // this isn't working because it is outisde of the loop,
    // but you don't need it.
    {
    return a;
    }
    */
    		return a; // just return a when the loop finishes, you don't need an if statement
    	}
    
    
    
    
    }
    
    int main()
    {
    int array[10]={-1, 5, -23, 90, 4, 5, 6, -2, 5, 9};
    cout<<smallest(array, 10);
    return 0;
    }
    Last edited by Recursive; 06-03-2012 at 05:32 AM.

  5. #5
    Join Date
    Jan 2011
    Posts
    151
    Mentioned
    0 Post(s)
    Quoted
    8 Post(s)

    Default

    /*big huge block
    gets commented
    */
    use /* stuff you want to comment and end it with */

    e2: when I run it I get some weird ass number -987453 which is smallest[10]/smallest[i+1] which is not in the array

    if you are trying to find the smallest number in the array you need to do something like this

    int smallest(int array[], int size)
    {
    int a = 0;
    if (size ==1)
    {
    return array[0];
    }
    else
    {
    for (int i = 0; i < size; i++)
    {
    if (array[i] < a)
    {
    a = array[i];


    }
    return a;
    }
    also in yours you have
    for (int i = 0; 1 < size-1; i++)
    it should be "i <size-1" not "1 < size-1"

  6. #6
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Quote Originally Posted by boobooo42 View Post
    also in yours you have.....

    it should be "i <size-1" not "1 < size-1"

    That doesn't work.. See my edited post for the solution. If you don't want to scroll up then:

    C++ Code:
    int Smallest(int Array[], int size)
    {
        if (size == 1)
            return Array[0];

        int A = Array[0];
        for (int I = 0; I < size; I++)
            A = Array[I] < A ? Array[I] : A;

        return A;
    }
    Last edited by Brandon; 06-03-2012 at 06:15 AM.
    I am Ggzz..
    Hackintosher

  7. #7
    Join Date
    Jan 2011
    Posts
    151
    Mentioned
    0 Post(s)
    Quoted
    8 Post(s)

    Default

    Yeah, brandon's works... I am not very familiar with using ? so props for that. and yeah value A needs to be initialized to array[0] not 0, sorry.

  8. #8
    Join Date
    Dec 2011
    Location
    -bash
    Posts
    515
    Mentioned
    0 Post(s)
    Quoted
    27 Post(s)

    Default

    Yup just tried Brandon's, I still get -100...ffs. Is it just my compiler or is someone else trying this getting the right answer?

    EDIT: btw what is the question mark for? I have never used it before so I am curious. How would one interpret this block of code?
    C++ Code:
    A = Array[I] < A ? Array[I] : A;
    Last edited by Recursive; 06-04-2012 at 03:56 AM.

  9. #9
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Quote Originally Posted by Chig View Post
    Yup just tried Brandon's, I still get -100...ffs. Is it just my compiler or is someone else trying this getting the right answer?

    EDIT: btw what is the question mark for? I have never used it before so I am curious. How would one interpret this block of code?
    C++ Code:
    A = Array[I] < A ? Array[I] : A;

    Uhh it works for me :S

    The question mark's Tenary Conditional operator aka: short-hand if-else statements and returns a value.

    The following is the representation of the Short-hand posted before.
    C++ Code:
    If (Array[I] < A)
        A:= Array[I]
    else
       A:= A;

    Left side of the colon is true, right side is false. So If Array[I] < A then return the left side else return the right side.

    Thus A will be assigned the result of the short-hand.
    Last edited by Brandon; 06-04-2012 at 07:24 AM.
    I am Ggzz..
    Hackintosher

  10. #10
    Join Date
    Dec 2011
    Location
    -bash
    Posts
    515
    Mentioned
    0 Post(s)
    Quoted
    27 Post(s)

    Default

    man you just showed me a lot of things about C++ that I never knew. First you don't need the curly braces after each condition to write a statement, then the question mark thing and now you can assign to a variable using (:=) kinda like in pascal.

    I guess pascal and cpp have something in common afterall.

    But Brandon since you are still helping me, I want you to look at this function I made in place of yours. I don't need any fancy shortcuts, just look at it and tell me a way to make it work. I don't like copying other people's work, I like to learn it myself:

    C++ Code:
    #include <iostream>
    using namespace std;

    int Smallest(int array[], int size)
    {
        int s;
        int t;
        int small;
        //int newarray[size];
        if (size == 1)
        {
            return array[0];
        }
        else
        {
            for (int b = 0; b < size; b++)
            {
                while((b + 1) <= size)
                {
                    if (array[b] < array[b + 1])
                    {
                        s = array[b];
                    }
                    else
                    {
                        t = array[b + 1];
                    }
                    if (s < t )
                        small = s;

                    else if (t < s)

                        small = t;
                }

            }

            return small;

            /*if (s < t)
                {
                    return s;
                }
                else
                {
                    return t;
                }*/



        }


    }


    /*int Smallest(int Array[], int size)
    {
        if (size == 1)
            return Array[0];

        int A = Array[0];
        for (int I = 0; I < size; I++)
            A = Array[I] < A ? Array[I] : A;

        return A;
    }*/




    int main()
    {
        int numbers[10]={-1, 5, -23, 90, 4, 5, 6, -2, 5, 9};
        cout<<"The smallest number in the array is " <<Smallest(numbers, 10);
        return 0;
    }

    E: Nvm Brandon. I used the solution you posted above and it worked perfectly. Now I'm gonna try something harder by making it find numbers in the array that add up to another number.
    Last edited by Recursive; 06-04-2012 at 06:45 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •