|
-
Sort function for an array.
Hi I have the following code, in C++ that I did.
void sortit(char [5][5], int, int);
#include <stdio.h>
#include <iostream.h>
int main()
{
char names[5][5] = {{'C','a','r','l'},
{'J','a','m','e','s'},
{'J','a','n','e','t'},
{'A','l','e','x'},
{'K','a','r','e','n'}};
int i,j;
char a[10][10] = {'C'};
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
printf("%c",names[i][j]);
printf("\n");
}
sortit(names,i,j);
return 0;
}
//
//
// Modify this sortit function and place it here
//
//
void sortit(char unsorted[5][5], int i, int j)
{
//declare new arrays for multiplier and values and also temps for storage
long int values [5][2] = {{0,0},{1.0},{2,0},{3,0},{4,0}};
long int multiplier [5] = {10000, 5000, 1000, 500, 1};
long int T1, T2;
for(i=0; i<5; i++)
for(j=0;j<5;j++)
values[i][1] += unsorted[i][j] * multiplier [j];
for(i=0; i<5; i++)
for(j=i+1; j<5; j++)
if(values[i][1] > values[j][1])
{
T1 = values[i][0];
T2 = values[i][1];
values[i][0] = values[j][0];
values[j][1] = values[j][1];
values[j][0] = T1;
values[j][1] = T2;
}
for(i=0; j<5; i++)
{
for(j=0; j<5; j++)
cout << unsorted [values[i][0]][j];
cout << "\n";
}
}
This is supposed to sort the names in alphabetical order but its not, its only repeating the names in the original order. I dont know what else to do.
------------------
1.33GHz Athlon
512MB DDR
Epox 8-K7A
2 * 40GB IBM HD's ATA-100
Gainward Geforce 2 Ti 450
SBLive Value
Pioneer 16x DVD Slot Load
Enlight 7237 300W Case
Lite-On 16102B
Microsoft Optical Mouse
Microsoft Natural KeyB
3Com 3C905
Viewsonic E95 19inch
A64
ASRock 939 Dual SATA
Corsair Value Select 1024MB
Gigabyte 7600GT
Audigy 2
NEC 2510A
Microsoft Optical Mouse
Microsoft Natural KeyBoard
Samsung 710N-2
-
**** dude, what are the odds? My friend didnt finish a C++ lab last night for class, so i write it for him. Took me like 5 mins... Here is the code I used to sort a vector.
Code:
//#####################################
//#
//# First Name Last Name - VC6++
//# Vector Sorting - 01/30/02
//#
//#####################################
//#
//# With help from Mike Spiegle
//#
//#####################################
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void Swap(string &, string &);
void PrintArray(vector<string> );
void BubbleSort(vector<string>&);
int main()
{
vector<string> Cities;
string Temp[] = {"Los Angeles", "San Diego", "San Francisco", "Las Vegas", "Manhattan Beach", "Austin", "Redondo Beach", "Torrance"};
int TempSize = 8;
int temp;
int temp2;
for(temp = 0; temp < TempSize; temp++)
{
Cities.resize(Cities.size() + 1);
Cities[temp] = Temp[temp];
}
cout<<"Before the Swap:"<<endl;
cout<<"================"<<endl;
PrintArray(Cities);
cout<<endl;
BubbleSort(Cities);
cout<<"After the Swap:"<<endl;
cout<<"==============="<<endl;
PrintArray(Cities);
return 0;
}
void Swap(string &first, string &second)
{
string temp;
temp = first;
first = second;
second = temp;
}
void PrintArray(vector<string>Cities)
{
int temp;
for(temp = 0; temp < Cities.size(); temp++)
{
cout<<Cities[temp]<<endl;
}
}
void BubbleSort(vector<string> &Cities)
{
int temp;
int temp2;
for(temp = 0; temp < Cities.size(); temp++)
{
for(temp2 = 0; temp2 < Cities.size(); temp2++)
{
if(Cities[temp2].compare(Cities[temp]) == 1)
{
Cities[temp].swap(Cities[temp2]);
}
}
}
}
-
Thanks for the code but I cant use that beacuse I have to use the code in the first function of my code that was supplied by the teacher, so it has to work with that.
------------------
1.33GHz Athlon
512MB DDR
Epox 8-K7A
2 * 40GB IBM HD's ATA-100
Gainward Geforce 2 Ti 450
SBLive Value
Pioneer 16x DVD Slot Load
Enlight 7237 300W Case
Lite-On 16102B
Microsoft Optical Mouse
Microsoft Natural KeyB
3Com 3C905
Viewsonic E95 19inch
A64
ASRock 939 Dual SATA
Corsair Value Select 1024MB
Gigabyte 7600GT
Audigy 2
NEC 2510A
Microsoft Optical Mouse
Microsoft Natural KeyBoard
Samsung 710N-2
-
geez... that code looks like **** to me, lol. Maybe if you could understand what it is im doing w/ my code, you can integrate>?
-
lol it may be **** but thats what he gave us to work with hehe, to make us think I guess. lol
------------------
1.33GHz Athlon
512MB DDR
Epox 8-K7A
2 * 40GB IBM HD's ATA-100
Gainward Geforce 2 Ti 450
SBLive Value
Pioneer 16x DVD Slot Load
Enlight 7237 300W Case
Lite-On 16102B
Microsoft Optical Mouse
Microsoft Natural KeyB
3Com 3C905
Viewsonic E95 19inch
A64
ASRock 939 Dual SATA
Corsair Value Select 1024MB
Gigabyte 7600GT
Audigy 2
NEC 2510A
Microsoft Optical Mouse
Microsoft Natural KeyBoard
Samsung 710N-2
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|