-
Array problems
Hi all C++ gurus
i have a little problem with my codes
basically i have 5,6,7,8,9 in my array
i m supposed to come up with a program that will run through the numbers in the array...if any number that is more than 7 will be replaced by 7
therefore it should look like this
original: 5,6,7,8,9
sorted: 5,6,7,7,7
however when i run
it looks like this
original: 5,6,7,8,9
sorted 5,7,7,7,0
can anyone pls help with me this becoz i m rather new in c++??
thanks :)
Code:
#include <vcl.h>
#include <condefs.h>
#include "MT262io.h"
#define Size 5
#pragma hdrstop
void displayArray (int Array[Size]);
//---------------------------------------------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{
int MyArray[Size] = {5,6,7,8,9};
int TopOfUnsorted;
int IndexOfLargest =7;
int IndexOfSmallest;
int Index;
int TempInt;
WriteString("Original Array : ");
displayArray(MyArray);
for (TopOfUnsorted=Size-1; TopOfUnsorted>0; TopOfUnsorted = TopOfUnsorted-1)
{
for (Index=0; Index<=TopOfUnsorted; Index = Index + 1)
{
if (MyArray[Index] >= MyArray[IndexOfLargest])
{
Index = IndexOfLargest;
}
}
//-- swap the position of the largest element with the last
//-- element in the unsorted part of the array
TempInt = MyArray[TopOfUnsorted];
MyArray[TopOfUnsorted] = MyArray[IndexOfLargest];
MyArray[IndexOfLargest] = IndexOfLargest;
}
WriteString("Sorted array : ");
displayArray(MyArray);
getchar();
return 0;
}
void displayArray(int Array[Size])
{
int Index;
for (Index=0; Index<Size; Index = Index + 1)
{
WriteIntPr(" ", Array[Index]);
}
WriteString("\n");
return;
}
-
> int IndexOfLargest =7;
But you only have 5 elements in your array, so using this as an index is a bad idea.
-
but my intention is to compare all the values in the array to 7
all those numbers that is larger than 7 will be converted to 7...
can anyone advice how i can do that??
-
Are there any restrictions on your assignment; as far as what you HAVE to have included? Or do you just need to write code to convert 5, 6, 7, 8, 9 to 5, 6, 7, 7, 7?
-
> but my intention is to compare all the values in the array to 7
Yeah, you said that already.
But look at your code
You have things like this
MyArray[Index] >= MyArray[IndexOfLargest]
Which on the first pass of your code reduces to
MyArray[0] >= MyArray[7]
Your code is now dead in the water, you just accessed an out of bound array element.
And also your swap code
Code:
TempInt = MyArray[TopOfUnsorted];
MyArray[TopOfUnsorted] = MyArray[IndexOfLargest];
MyArray[IndexOfLargest] = IndexOfLargest; // Duh?? should be TempInt?
You're not storing back into the array a value you read from the array.
This is why it fills up with 7's
-
thank you all so much
but still even when i managed to compiled
i still don't get the result i wanted....
i set min =2; max= 8
so my original array {0,1,2,3,4,5,6,7,8,9)
shd look like this when i run the function
{2,2,2,3,4,5,6,7,8,8}
but i get
{8,8,8,8,8,8,8,8} lol
can anyone please enlighten me??
thanks
Code:
#include <vcl.h>
#include <condefs.h>
#include "MT262io.h"
#define Size 10
#pragma hdrstop
void setArray(int NumberArray[10]);
void MinMaxLimit(int min, int max,int NumberArray[10]);
float Average (int NumberArray[10]);
void displayArray(int NumberArray[10]);
//---------------------------------------------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{
int MyArray[10] = {0,1,2,3,4,5,6,7,8,9};
int minimum =2;
int maximum =8;
MinMaxLimit(minimum, maximum ,MyArray);
displayArray(MyArray);
WriteStringCr (Average (MyArray));
getchar(); //-- hold the screen
}
void setArray(int NumberArray[10])
{
int num[10] = {0,1,2,3,4,56,7,8,9};
setArray(num);
return;
}
void MinMaxLimit(int min, int max,int NumberArray[10])
{
int counter;
for(counter =0; counter <10; counter ++)
{
if (NumberArray[counter] < min)
{
min =min +1;
}
if (NumberArray[counter] > max )
{
max =max +1;
}
counter = counter +1;
}
}
float Average (int NumberArray[10])
{
int total;
float average;
int counter;
for(counter =0; counter <10; counter ++)
{
total = total + NumberArray[counter];
}
average = total/10;
return average;
}
void displayArray(int NumberArray[10])
{
int Index;
for (Index=0; Index<Size; Index = Index + 1)
{
WriteIntPr(" ", NumberArray[10]);
}
WriteString("\n");
return;
}
-
I'll assume the undefined functions are implemented somewhere in the headers you include.
As for the actual program, I have no idea how your array ends up as all 8s since you never change the value of an element of your array. Most likely, it happens in one of those other functions.
I hope you're not thinking MinMixLimit changes appropriate elements to the min or max. Look again at that function and tell me - what does it do?