Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 13th, 2005, 12:28 PM   #1
hopeolicious
Newbie
 
Join Date: Feb 2005
Posts: 8
Rep Power: 0 hopeolicious is on a distinguished road
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;
}
hopeolicious is offline   Reply With Quote
Old Mar 14th, 2005, 11:41 PM   #2
hopeolicious
Newbie
 
Join Date: Feb 2005
Posts: 8
Rep Power: 0 hopeolicious is on a distinguished road
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;
}
hopeolicious is offline   Reply With Quote
Old Mar 15th, 2005, 12:47 AM   #3
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
#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;
            }
}
__________________

tempest is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:44 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC