SRL Archive - C/C++ Help and Tutorials

[NEED HELP] C++ Looping


Recursive

2012-06-03 00:03:37

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.

#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;
}

Brandon

2012-06-03 00:12:53

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


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;
}

boobooo42

2012-06-03 01:06:55

#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 < 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;
}
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 to 'a' every time, not compare it to [i+1].

Recursive

2012-06-03 01:28:09

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]

#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;
}

boobooo42

2012-06-03 01:40:02

/*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 < a)
{
a = array;


}
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"

Brandon

2012-06-03 02:10:30

From: boobooo42

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:


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;
}

boobooo42

2012-06-03 12:23:04

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.

Recursive

2012-06-03 23:23:44

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?
        A = Array[I] < A ? Array[I] : A;

Brandon

2012-06-04 02:31:26

From: Chig
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?
        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.

If (Array[I] < A)
A:= Array[I]
else
A:= A;

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

Thus A will be assigned the result of the short-hand.

Recursive

2012-06-04 14:32:03

:wow: 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:

#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.