To begin this is my first time using these forums. I am a C++ Newbie, I am in college taking a C++ Course and I am having trouble. My teacher knows the subject, but I feel he has a hard time getting it across to us the class. So I am asking for help clarifying some of the things he is trying to convey to us. Also I work all the time and have no time to visit him in his office to ask for further explanation. YES the following code is from my homework, NO I do not expect you to do my homework for me. But help is needed despreatly.
This is the code that he gave to us, he asked us to make the fucntions into templates which I have done and tested by switching from an array of "ints" to an array of "chars" it still reads, sorts and prints so what I have changed on this version so far works, I have two other versions for the rest of the assignment that no longer work. here is the code as it stands, I will put the directions for completing the assignment after the code allong with what I would like as far as help goes...Again I DO NOT Expect you to do my homework I am simply trying to learn... SRY for the length.
The directions for me to complete the homework,
using namespace std;
template <class t> void printlist(ostream &os, t list, int count);
// prints list one item per line. user must
// make sure that argument os has been opened in case
// argument is a file stream and count is number of items
// stored in arguemtn list
template <class t> void printlist(ostream &os, t list, int count)
for(i=0; i<count; i++)
os << list[i] << endl;
} // End Printlist
template <class t> void readlist(istream &is, t list, int &count, int max_to_read);
// list is an array of integers, max_to_read is maximum number of
// items that can be stored in list. on return count is set to
// actual number of items read from is into argument list.
// user must make sure that is has been opened in case of an input
// file stream. Reads until max_to_read items have been stored in
// array or end of file whichever comes first.
template <class t> void readlist(istream &is, t list, int &count, int max_to_read)
is >> temp;
count = 0;
while(count < max_to_read && !is.eof())
list[count++] = temp;
is >> temp;
} // End Readlist
template <class t> void sortlist(t list, int count);
// sorts list into ascending order. count is number of items stored in
//array uses selection sort algorithm
template <class t> void sortlist(t list, int count)
// make count-1 passes through list
// note that count is decremented by 1 to save an operation
// of subtracting one in the for header, which normally reads
// pass< count -1. In the code to complete a pass through the
// list then to go to the end of the array, i must index
// from pass + 1 to count, which since decremented is the
// last position in the list where data are stored
for(pass=0; pass<count; pass++)
minpos = pass;
for(i=pass+1; i<=count; i++) // note above
if (list[i] < list[minpos])
minpos = i;
if(minpos != pass) // save a swap maybe?
} // End Sortlist
const int array_size = 20;
cout << "Enter integers (max 20) using ^D to terminate" << endl;
readlist(cin, list, count, array_size);
cout << "Done reading, sorting..." << endl << endl;
cout << "Sorted list" << endl;
printlist(cout, list, count);
cout <<"Done printing list. Total of " << count << " items."<< endl;
} // End Main
1.) make the three functions included here generic. (Which I have done and tested so that part works)
2.) Then in the same program declare two arrays. One an array of floats and the other an array of (single) characters. Read the array of floats, sort and print. Then in the same program read the array of chars, sort and print. Note that since the input function reads until eof or the array is full, you will probably have to call cin.reset() between reads to reset the end of file flag.
I see that the prog passes the array called "list" to the functions. I tried declaring two arrays one for "chars" and one for "floats" named "list" but of course I couldnt do that. so my question is, how do I declare 2 arrays of different types that I can pass through the functions. I have 2 other versions of this prog where I have modified the code so much that I am confused with them so I am dropping back and starting again.
If you could either write an explanation in simple terms and or a small snippet of code to show an example of how this would be done so that I can take it and run with it to complete my homework. Please keep it simple, I am having a very hard time with this class and am in need of help. Again I appologize for the length of the post.