Hi all,

I need some help finishing this program, I know it's easy but I'm new in C++ and programming.

#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
const int arraysize = 10;
int a[arraysize] = {11,22,33,44,55,66,77,88,99,100};

This is the start of my program ; a giving array which a want the resault to be the highest and the lowest numbers in it like this:

Highest number is: ******
Lowest number is: ******

If somebody can finish the code for me, I'll appreciate it.

Is the array always sorted from lowest to highest?

Easy way in my opinion is to create a variable such as highNum = 0, iterate through every index in the array, if the element of the index in the array is HIGHER than high num, then set highNum = a[index_num];
Same for lowNum, expect make sure you set the if condition to lowNum > a[index_num] or swap the statement, both work. Some might find it even easier to bubble sort the array then use maxElements -1, in your case 9, for the highest number, then 0 for the lowest, but I think that is a bit complicated for a beginner :D.

Is the array always sorted from lowest to highest?

No, it's just an example to explain that it's 10 numbers and it's already given (the user doesn't enter them).

Easy way in my opinion is to create a variable such as highNum = 0, iterate through every index in the array, if the element of the index in the array is HIGHER than high num, then set highNum = a[index_num];
Same for lowNum, expect make sure you set the if condition to lowNum > a[index_num] or swap the statement, both work. Some might find it even easier to bubble sort the array then use maxElements -1, in your case 9, for the highest number, then 0 for the lowest, but I think that is a bit complicated for a beginner :D.

It's a little complicated for me :(

Can you give me a code example for the first 3-4 numbers and I finish it?

Give us a general idea of your level of understanding with programming.

do you know how to compare 2 numbers? (if..else statement)

lowNum > a[index_num]

do you know how to assign values to variables ?(int i = 0; ... i = 10; )
if you can do these two things then you should be able to figure this problem out yourself... don't forget we can't do your homework for you.

Give us a general idea of your level of understanding with programming.

do you know how to compare 2 numbers? (if..else statement)
do you know how to assign values to variables ?(int i = 0; ... i = 10; )
if you can do these two things then you should be able to figure this problem out yourself... don't forget we can't do your homework for you.

I'm a starter at C++, I know those two things in .NET programming (Windows Applications in Visual Studio) and I think I can make them into work in C++ too, but I'm missing the idea because it's 10 numbers and doing an IF ... Else statement between every 2 numbers will be too long !

I can do it if it's between 2 numbers, but more ; I don't know what's the way to do it. Should I put it like this:

#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
const int arraysize = 10;
int a[arraysize] = {11,22,33,44,55,66,77,88,99,100};
int max = a[0];
{if max > a[1]
max = max
else
max = 1[0]
{if max > a[2]
max = max
else
max = a[2]
}
{if max > a[3]
max = max
else
max = a[3]
}
}
cout << "Biggest number is:" << max;

Or how ?

Ohh..sorry my bad i forgot the for loop.

does this look familiar?...

for(int i = 0; i < arraysize; ++i)

Ohh..sorry my bad i forgot the for loop.

does this look familiar?...

for(int i = 0; i < arraysize; ++i)

Yeah it does, but help me on it a little bit cuz loop is kinda my weakness ; I dunno how to work it out. Should I put it before an IF statement and it will loop for the whole numbers inside the array?

this is what should be going on:

(1)get 1st number in the array and store it
(2)get 2nd number in the array and compare with stored number
(3)if the number is larger...then store the 2nd number
(4)else keep looping (do nothing)
(5)get 3 number and compare with stored number...............

now where do you think the for loop starts... before or after step 1?
and where is the if statement?

this is what should be going on:

(1)get 1st number in the array and store it
(2)get 2nd number in the array and compare with stored number
(3)if the number is larger...then store the 2nd number
(4)else keep looping (do nothing)
(5)get 3 number and compare with stored number...............

now where do you think the for loop starts... before or after step 1?
and where is the if statement?

I think the loop will start after step 1, and the IF statement will be start in steps 2,3 ; in comparing which is the higher number.

Yes that is correct.. now you just need to put that in code. Heres a clue:

int largest = a[1];
	for(.....)
	{
		if(something  > something)
			something = something;
	}

Yes that is correct.. now you just need to put that in code. Heres a clue:

int largest = a[1];
	for(.....)
	{
		if(something  > something)
			something = something;
	}

Thanks, this is enough for me ; I'll work on the rest now everything is clear. Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.