## power_computer 20

I have two arrays both have a defined sized for each element that was filled when reading from a file currently but have a max size of 50

I have array 1 [4]
I have array 2 [2]

However, my main point is I have to process these two arrays and print them out all simtaneously as one list based on ascending value for the element in the array

So it is not as simple as two for loops and printing the array, I have to sort the array in ascending order when it is printed

How would I do this ? with some sort of multiple for loop initialization ?

## hmortensen 13

If I understand what you want to do, I would recomend you take a look at merge sort.

You can use this algorithm to eighter make a new array containing the result and then print it out, or print out the values as you do the merge sort.

I found this to be a good explanation of the merge sort algorithm.

Hope this was of some help to you,

- Henning

## power_computer 20

Thanks for the help Ill add a bit of spefics

Each class is of a type defined by a type of either two classes Ive made

Class 1 array
Class 2 array

They each contain a date value which I look at to print out both arrays simultaneously in ascending order for the class array which contains a date, amount, and etc. so Im not just printing out a date

## Nick Evan 4,005

You could make the search-algorithm yourself (and that would be fun), but you can also save yourself alot of trouble and let std::sort do it for you.
- load all numbers in a vector with two loops
- std::sort them
- print them out
- done

for example:

``````#include <iostream>
#include <vector>
#include <algorithm>

int main(){
int a[2] = {4,8};
int b[4] = {6,2,1,6};

std::vector<int> all_numbers;
for (unsigned i = 0; i < 2; ++i) all_numbers.push_back(a[i]);
for (unsigned i = 0; i < 4; ++i) all_numbers.push_back(b[i]);

std::sort(all_numbers.begin(), all_numbers.end());

for (unsigned j = 0; j < all_numbers.size(); ++j) std::cout << all_numbers.at(j);
}``````

## power_computer 20

^ I suppose the information above of your post would be of some help in aiding me

I dont believe we can use vectors as we have not covered them yet and are stated too use arrays and such and process them at the same time

## Nick Evan 4,005

Then you still need to load them all in one array to sort them. Here's a great site on sorting. I would recommend selection-sort, because it's very easy to implement and you don't have much number to sort.

## firstPerson 761

You could make the search-algorithm yourself (and that would be fun), but you can also save yourself alot of trouble and let std::sort do it for you.
- load all numbers in a vector with two loops
- std::sort them
- print them out
- done

for example:

``````#include <iostream>
#include <vector>
#include <algorithm>

int main(){
int a[2] = {4,8};
int b[4] = {6,2,1,6};

std::vector<int> all_numbers;
for (unsigned i = 0; i < 2; ++i) all_numbers.push_back(a[i]);
for (unsigned i = 0; i < 4; ++i) all_numbers.push_back(b[i]);

std::sort(all_numbers.begin(), all_numbers.end());

for (unsigned j = 0; j < all_numbers.size(); ++j) std::cout << all_numbers.at(j);
}``````

You can get rid of one of the loops.

``````std::vector<int> all_numbers(a,a+2);
for (unsigned i = 0; i < 4; ++i)
all_numbers.push_back(b[i]);``````

## power_computer 20

Even If I load them into one array the two arrays are of different types so elements in the array will vary

what kind of array would I load them into ? each different class array contains atleast 4 -5 values in each element

perhaps an array of structs? making another array seems like a hassle

## Nick Evan 4,005

the two arrays are of different types

So? You'll have to sort them on something they have in common right? Like a date, or an ID?

## VernonDozier 2,218

Even If I load them into one array the two arrays are of different types so elements in the array will vary

what kind of array would I load them into ? each different class array contains atleast 4 -5 values in each element

perhaps an array of structs? making another array seems like a hassle

Well, you're going to have to have some underlying type for the resulting array and you're going to decide what that type is. The larger array needs to be able to handle either type. If you're merging a double array and an integer array, the resulting array that needs to be sorted should be an array of doubles. If you're merging an array of floats and an array of integers, there are integers that cannot be represented as a float and vice versa, so again, convert each to doubles. structs could be all sorts of things, depending on the struct. Since you haven't told us what you're sorting, it's hard to speculate. And as niek_e mentions, if it's a struct, you'll have to define your own comparison function.

## power_computer 20

This seems to be more a thinker than I thought well me spefciy the whole problem

I have currently 2 classes which I made an array of there objects
So I have Class 1 Array and Class 2 Array

I have to print these two arrays out simtaneuosly however in ascending order

The contents of each array is as suck

Class 1 Array (int,float,string,string,int)
Class 2 Array (int,float,string,int,string)

The value that determines the ascenation is the first int value which is the day value. That is why im seeing that a whole array wont work however the second string in array 1 is just number but the second string in class 2 is text and numbers so its a bit of a mix up

## power_computer 20

Sighhh...bump. Im trying an array of structs with some simple conversions for reach array but it is not working quite well so help?

## VernonDozier 2,218

Sighhh...bump. Im trying an array of structs with some simple conversions for reach array but it is not working quite well so help?

You haven't posted any code, so we have no idea what you're doing. First things first. Forget the merge, forget the sort. You have three arrays. How are they defined? What are the struct definitions? How many do you have. Presumably you could be using polymorphism where you have a parent A class and child B and C classes, and you're merging the arrays of type B and C into an array of type A. But I'm just speculating that that's what you're doing. I think you are going to have to provide many more details and some code for people (or at least me!) to understand.

## power_computer 20

Ok well I managed to devise a sloppy code using an array of structs to stick my two class arrays into one ill post the code and try to explain what I have done

``````Where i and j are the size of the array classes, sloppy mess I know
struct list
{
int day;
float amount;
string custNumber;
int info; //based on observations both Item Number and empNumber are planly integers
string info2;  //This will help place my values in the elements into an array of structs for easier printing
};

list print[100]; //Array of structs
stringstream ss[100]; //Array of sstream conversion of int to string
int k, l;

//Initialize array with service transaction array

for(k = 0; k < j; k++)
{
print[k].day = stran[k].GetDay();
print[k].amount = stran[k].GetAmount();
print[k].custNumber = stran[k].GetCust();
print[k].info = stran[k].GetEmpNumber();
print[k].info2 = stran[k].GetDescription();
}

//Fill in the rest of the array with merchandise array, we start where the first fill stopped
for(l = k; l < i + k; l++)
{
print[l].day = mtran[l - k].GetDay();
print[l].amount = mtran[l - k].GetAmount();
print[l].custNumber = mtran[l - k].GetCust();
print[l].info = atoi(mtran[l - k].GetItemNumber().c_str());
//Simple conversion of int to string using sstream
ss[l - k] << mtran[l-k].GetQuantity();
print[l].info2 = ss[l-k].str();

}``````

## VernonDozier 2,218

What's stran and mtran? You don't post their types, which was the whole point. In an example like this, you must post everything that's relevant and nothing that's not relevant. That means if you have a class, just have the data members, make 'em public so you don't need getters and setters, and get rid of all the functions, save for possibly a constructor and destructor. Everything else may be relevant for something else, but not for the problem you are talking about.

You have an array of stringstreams? I thought you said you had two very similar, but not exactly the same structs, one being the list struct, the other being something very similar to the list struct, but not exactly the same, one array being of type list, one array being of the type of something close to list, and the third array being some type which I wasn't clear on, but never guessed it would be an array of stringstream. I'm not sure if the stringstreams represent here. If you are just using them them to fill in the OTHER arrays and they have no use beyond that, again that just adds complexity that may be of use in your actual program, but just muddies up the example. Hard code some values into the initial arrays so it's obvious.