English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

i input 10 integers and the program will find it's higest number and lowest number. i already have the code for inputting 10 integers and finding it's highest and lowest number but i dont know how to do this with array.

how can i insert array into my code?

this is the code:

main()
{
int myI;
int hi, low;
int i;

clrscr();
for(i=1; i<=10; i++)
{
printf("Enter integer #%d:",i); scanf("%d",&myI);
if(i==1)
{
hi=myI;
low=myI;
}
if(myI>hi)
{
hi=myI;
}
if(myI {
low=myI;
}

}
printf("The highest integer is %d\n",hi);
printf("The lowest integer is %d\n",low);
getch();
}
thnx

2007-12-12 02:05:36 · 3 answers · asked by Mikaela 1 in Computers & Internet Programming & Design

3 answers

Define your array
int myArray[10];
myArray has elements [0] to [9]
The last element is always [array length - 1]
So your for loop needs to be
for (i=0; i<10;i++)

Input your numbers and assign them to myArray[i]

Change all other variables to use myArray[i] and it should work.

Good luck.

2007-12-12 02:37:52 · answer #1 · answered by AnalProgrammer 7 · 0 0

Is this Java? It kinda' looks like it. Anyhoo, basically, you need to

- instantiate an array with 10 postions in the declaration section. if Java, like this
int myNums[ ] = new int[10]

- inside the loop where you ask for the number, assign the entry to the corresponding position in the array, like so
myNums[ i - 1] = myl
you need to have the 'i-1' because arrays are 0 based

- once you have all the numbers in the arra, you will need to sort it. I will let you figure this part out - there may be a 'sort' method to the Array class in Java. if not, you may want to research 'bubble sorts'

Good luck, and happy coding : )

2007-12-12 10:35:12 · answer #2 · answered by Anonymous · 0 0

int main()
{
int myBuffer[10];
int myI;
int hi;
int low;

for(int i =0; i < 10; i++)
{
printf("Enter integer #%d:",i); scanf("%d",&myI);
if(i == 0)
hi = low = i;

myBuffer[i] = myI;
if (myBuffer[hi] < myI;
hi = i;
else if(myBuffer[low] > myI)
low = i;
}

printf("The lowest integer is: %d\n", myBuffer[low]);
printf("The highhest integer is: %d\n", myBuffer[hi]);

getch();

return(0);
}

2007-12-12 10:49:09 · answer #3 · answered by CatNip 6 · 0 0

fedest.com, questions and answers