![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2005
Posts: 8
Rep Power: 0
![]() |
selection sort
Can someone help me to make my program output each element in the array that is user prompted
so far it.... prompts the user for input no more tan 50 elements and a value less the 999 it doesnt .... output the elements and sorts then in acending order #include <iostream.h>
#include <iomanip.h>
int sort(int MYARRAY[], int p);
const int LIMIT = 50;
int MYARRAY[LIMIT];
int x,i,j,min,minx,temp,moves=0;
int value;
int main()
{
for(x=0;x<LIMIT;x++)
{
for(x=0;x<LIMIT;x++)
{
while(value < 999)
{
cout << "\n\nPlease enter a number: ";
cin >> MYARRAY[x];
value = value + MYARRAY[x];
moves = sort(MYARRAY, LIMIT);
}
cout << MYARRAY[LIMIT];
}
cout << "\n\nThe sorted list, in ascending order, is:\n";
cout << setw(4) << MYARRAY[LIMIT];
cout << endl << moves << " moves were made to sort this list\n";
}
return 0;
}
int sort(int a[], int n)
{
for(x=0;x<(LIMIT-1);x++)
{
min = MYARRAY[x];
minx = x;
for(j=x+1;j<i; j++)
{
if(MYARRAY[j] < min)
{
min = MYARRAY[j];
minx = j;
}
}
if (min < MYARRAY[x])
{
temp = MYARRAY[x];
MYARRAY[x] = min;
MYARRAY[minx]=temp;
moves++;
}
}
return moves;
} |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Feb 2005
Posts: 8
Rep Power: 0
![]() |
sort and output elements in array
can show me how can i get my program to sort an array of elements that are user input and output them
Code: #include <iostream>
using namespace std;
const int LIMIT = 50;
int main()
{
int MYARRAY[LIMIT];
int value;
int i = 0;
int print = 0;
bool exit = false;
while(!exit && i < LIMIT)
{
cout<<"Enter a number or 999 to quit ";
cin>>value;
if(value != 999)
{
MYARRAY[i] = value;
i++;
}
else
{
exit = true;//if value equals 999 exit the loop
}
}
while(print < i)
{
cout<<MYARRAY[print]<<endl;//print out the number as long as it is less than i
print++;
}
//"pause" the program
cin.ignore(80,'\n');
cin.get();
return 0;
} |
|
|
|
|
|
#3 |
|
Programming Guru
![]() |
#include <iostream>
#include <conio.h>
#define LIMIT 50
using namespace std;
int main() {
int MYARRAY[LIMIT];
char test;
std::cout << "For input, enter a number or 999 to exit: " << std::end;
for(int i=1;i<LIMIT+1;i++) {
strcpy(test, "");
while(atoi(test) == 0) {
printf(" number (#%d) -> ", i);
std::cin >> test;
}
if(atoi(test) != 999)
MYARRAY[i-1] = atoi(test);
else
break;
}
sort(MYARRAY);
for(i=0;i<LIMIT;i++)
printf("%d\n", MYARRAY[i]);
//"pause" the program
getch();
return 0;
}
void sort(int &nums[]) {
for(i=0;i<LIMIT-1;i++)
for(j=0;j<LIMIT-1-i;j++)
if(nums[j] > nums[j+1]) {
int tmp = nums[j];
nums[j] = nums[j+1];
nums[j+1] = tmp;
}
}
__________________
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|