c++: selectionSort with pointers instead of index - c++

I want to sort numbers using pointers instead of indexes. By the way this is the header file :
int * sort(const int * const array, int size)
And below is the source code I was given
void selectionSort(int list[], int arraySize)
{
for(int i=arraySize-1; i>=1; i--)
{
int currentMax=list[0];
int currentMaxIndex=0;
for(int j=1; j<=i; j++)
{
if(currentMax=list[j])
{
currentMax=list[i];
currentMaxIndex=j;
}
}
if(currentMaxIndex != i)
{
list[currentMaxIndex]=list[i];
list[i]=currentMax;
}
}
}
I know you can switch list[i] to *(list+i) but I don't know how to do it with "currentMaxIndex". I'd really appreciate your help!!

Pointers in C are integers like any others, and can be compared with the usual operations.
So you can do things like:
int *end = list + length; // Create a pointer to one-past the end of an array
for(int *it = list; it < end; it++){ /* use it to read the elements */ }
int *my_favorite_index = list + some_index;
I'm not going to solve your homework for you, but I hope this helps.

Related

Function and Array in C++: Unexpected output

I need some help here please.
I just started learning C++ (coming from Python background).
I'm trying to familiarize myself with arrays and functions. Wrote a bunch of functions to do as stated, above each one.
However, the function which is supposed to sum elements in an array and return their sum, seem to be adding 10 to the result, no matter the argument supplied as input. What am I doing wrong please, as I can't seem to find this out. Any help on general layout of my code also would be appreciated.
// WORKING WITH ARRAYS AND FUNCTIONS
#include<iostream>
using namespace std;
// FUNCTION TO INSTANTIATE ARRAY INT OF LENGTH N.
int* array_creator(int n)
{
static int ary_of_ten[10]; //declare array
for (int i=0; i<n; i++) //use loop to fill it up
{
ary_of_ten[i] = i+1;
}
return ary_of_ten;
}
//FUNCTION TO PRINT ARRAY ELEMENTS
void* array_printer(int arr[], int array_lenght)
{
for (int i=0; i<array_lenght-1; i++)
{
cout << arr[i] << " ";
}
cout << arr[array_lenght-1] << endl;
}
//FUNCTION ACCEPTS INT ARRAYS AND RETURNS ARRAY OF SQUARE OF EACH ELEMENT
int* square_array(int *p, int array_length)
{
const int ary_sz(array_length);
static int sqd_values[10];
for (int i=0; i<ary_sz; i++)
{
*(sqd_values + i) = *(p+i) * *(p+i);
}
return sqd_values;
}
//FUNCTION ACCEPTS INT ARRAYS AND RETURNS SUM OF ITS ELEMENTS
int sum_array(int *arry, int array_length)
{
int summation;
for(int i=0; i<array_length; i++)
{
summation += *(arry + i);
}
return summation;
}
int main()
{
cout << sum_array(array_creator(10), 3) << endl;
array_printer(array_creator(10), 10); //print array of 1-10 elements
array_printer(square_array(array_creator(10), 10), 10); //prt arry of sqrd values
return 0;
}
summation shuld be initialized to 0.
int summation=0;

Merging two sorted arrays with for loop

I have a function that merges two sorted arrays into one and returns a pointer to it. I want to use a for loop rather than a while. However in some test cases the last 1 or 2 elements of the merge array are not in their place. I would appreciate if someone can help solve this problem keeping the for loop.
int * mergeSort(int arr1[], int arr2[],int len)
{
/* len is the combined length of the two arrays */
static int sorted[100];
int pos1=0, pos2=0;
for (int i=0; i<len; i++)
{
if (arr1[pos1]<=arr2[pos2])
{
sorted[i]=arr1[pos1];
pos1++;
}
else
{
sorted[i]=arr2[pos2];
pos2++;
}
}
return sorted;
}
Your problem is that you don't seem to handle going past the end of the input arrays. If there is uninitialized memory - you get undefined behaviour.
You can avoid this by terminating your arrays with a sentinel value, for example INT_MAX, which should always be bigger than all possible values in the arrays:
int a[] = { 1, 2, 104, INT_MAX};
int b[] = { 101, 102, 105, INT_MAX};
int* ptr = mergeSort(a,b,6);
for(int i = 0; i < 6; i++){
cout << i << " " << ptr[i] << endl;
}
live demo
Or you can pass the actual lengths of both arrays and handle them correctly:
int * mergeSort(int arr1[], int len1, int arr2[],int len2)
{
/* len is the combined length of the two arrays */
static int sorted[100];
int pos1=0, pos2=0;
for (int i=0; i< len1 + len2; i++)
{
if ((pos2 == len2) || (arr1[pos1] <= arr2[pos2] && (pos1 < len1)))
{
sorted[i]=arr1[pos1];
pos1++;
}
else
{
sorted[i]=arr2[pos2];
pos2++;
}
}
return sorted;
}
live demo
This doesn't answer the question of what's wrong with your code, but to answer the question of how to merge two sorted ranges I would suggest std::merge:
int * mergeSort(int arr1[], int arr2[], int len1, int len2)
{
//I am not condoning the use of a static buffer here,
//I would probably use a std::vector or std::array,
//possibly a boost::static_vector if really necessary
static int sorted[100];
std::merge(arr1, arr1 + len1, arr2, arr2 + len2, sorted);
return sorted;
}
I also changed int len to int len1, int len2 because you need to know the lengths of the individual arrays, not just their combined length, to avoid reading past the end of the input arrays.

Return an array of compared char pointers in C++ [duplicate]

This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 8 years ago.
I'm at college and we're learning pointers. Our job was to input a char, compare it to an array and return a pointer to the first reference of that char in the array. But, as I don't like easy things, I've asked my teacher what about having that char more than once in the array.
That's where my headache begins.
So I have this code. The idea is: create a function that compares the input char to the entire array, get the pointers of the references and save them in an array and return that array.
Unfortunately it's not working as I wish :(
What can be wrong?
#include<iostream>
#include<cstdlib>
using namespace std;
char list [10];
int main()
{
initialize();
show();
cout<<search('1');
}
void initialize()
{
int i;
for(i=0; i<10;i++)
{
list[i]='1';
}
}
void show()
{
int i;
for(i=0; i<10;i++)
{
cout<<list[i];
}
}
int* search(char* input)
{
int* result[10];
int i;
char *temp;
for (i=0; i<10; i++)
{
*temp=list[i];
if(strcmp(temp, input) != NULL)
{
result[i]=i;
}
}
return result[];
}
I'm on a mobile device so I can't go into huge detail unfortunately, but you are returning a pointer to an array that you create in the function which goes out of scope at the end of the function.
My massive edit:
As everyone has already stated, a C++ array is actually only a pointer to the first element in the array. As a result, if you return a pointer to an array created in the scope of the function, you are returning a pointer to garbage. If I were doing this I would use a vector, but if I were to be forced into using an array, I would use something like the code below. Hope this helps!
#include <iostream>
#include <cstdlib>
void initialize(char* list) {
for(int i = 0; i < 10; ++i) {
if(i < 4) {
list[i] = '2';
} else {
list[i] = '1';
}
}
}
void show(char *list) {
for(int i = 0; i < 10; ++i) {
std::cout << list[i] << ' ';
}
std::cout << std::endl;
}
// Note the function requires an additional argument that is a pointer
// this is how you can avoid returning a pointer
int search(char input, char* list, char* result) {
int j = 0;
for(int i = 0; i < 10; ++i) {
// comparing characters can be done via ==
if(input == list[i]) {
*(result + j) = list[i];
// You could use result[j], but I used this to show that
// result really is just a pointer to the first element
// of the array. As a result saying result[j] is the same
// as saying I want to deference j elements past the first
// element or *(result + j)
++j; // increment j
}
}
// return how many elements matched
return(j);
}
int main(int argc, char *argv[]) {
char list[10];
char temp[10];
initialize(list);
show(list);
int size = search('1', list, temp);
// create a dynamically sized array containing space for each match
// because we don't know the size at compile time we must use
// a library type or a dynamically sized array
char *result = new char[size];
for(int i = 0; i < size; ++i) {
result[i] = temp[i];
// again you could use result[i]
std::cout << *(result + i) << std::endl;
}
delete[] result; // otherwise you'll get a memory leak
return(0);
}

Novice Coder here: C++ Copying vector into array using functions

Beginner here trying to understand the fundamentals of functions, passing my reference, and vectors/arrays.
My code reads a large data file into a vector. I then, somehow, need to convert the vector into an array, sort the array, and read the output. I believe my issue lies within my attempt to convert the vector to an array.
using namespace std;
//function prototype
int readInput(vector<int> &vect);
void sort(int[], int);
void showArray(int[], int);
int main()
{
vector<int> values;
int sum, avg;
sum = readInput(values);
const int SIZE = values.size(); //ISSUE LIES HERE
int arr[SIZE]; //and here
sort(arr, SIZE);
showArray(arr, SIZE);
avg = sum / values.size();
//cout << "The average is: " << avg;
return 0;
}
int readInput(vector<int> &vect)
{
int count;
int total = 0;
ifstream inputFile("TopicFin.txt"); //open file
if(!inputFile)
{
return 0; // if file is not found, return 0
}
while(inputFile >> count) //read file
vect.push_back(count); //add to file
for (int count = 0; count < vect.size(); count++)
total+=vect[count]; //sum data in vector
return total;
}
void sort(int array[], int size)
{
int startScan, minIndex, minValue;
for(startScan = 0; startScan < (size-1); startScan++)
{
minIndex = startScan;
minValue = array[startScan];
for(int index = startScan + 1; index < size; index++)
{
if (array[index] < minValue)
{
minValue = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minValue;
}
}
void showArray(const int array[], int size)
{
for(int count = 0; count < size; count++)
cout << array[count] << " " << endl;
}
You don't need to convert the vector to an array. You can sort the vector directly.
std::sort(values.begin(), values.end())
More info on sort here: http://www.cplusplus.com/reference/algorithm/sort/
I will add that in general, you should never use arrays, especially as a new C++ programmer. They are much more complicated than vectors, and are almost never useful in normal C++ code.
http://www.parashift.com/c++-faq/arrays-are-evil.html
Let me preface by saying that, while this is a good thing to do for learning, converting vectors to arrays is probably not something you should do in real code. In reality, you'd use std::sort to sort your vector.
The root of the issue is that you can't declare an array of size that is unknown at compile-time with the int arr[SIZE] syntax.
const int SIZE = values.size();
The value of this is known when the code is executed, but not at compile time. Therefore int arr[SIZE]; cannot work unlike, say, int arr[100]. To declare an array for which you know the size at runtime, you can do it dynamically like
int* arr = new int[size];
and then you are also forced to delete the array manually.
As seanmcl said, you don't need to convert to an array in order to sort. However, if what you want to do is an exercise in writing a sorting function, then you could simply use values.begin() since the elements of vectors are contiguous. (This is not true for other containers.)

implementation counting sort

here is code for counting sorting
#include <iostream>
using namespace std;
int main(){
int a[]={2,3,1,2,3};
int n=sizeof(int)/sizeof(int);
int max=a[0];
for (int i=1;i<n;i++) {
if (a[i]>max) {
max=a[i];
}
}
int *output=new int[n];
for (int i=0;i<n;i++) {
output[i]=0;
}
int *temp=new int[max+1];
for (int i=0;i<max+1;i++) {
temp[i]=0;
}
for (int i=0;i<n;i++){
temp[a[i]]=temp[a[i]]+1;
}
for (int i=1;i<max+1;i++) {
temp[i]=temp[i]+temp[i-1];
}
for (int i=n-1;i>=0;i--) {
output[temp[a[i]]-1]=a[i];
temp[a[i]]=temp[a[i]]-1;
}
for (int i=0;i<n;i++) {
cout<<output[i]<<" ";
}
return 0;
}
but output is just 2,only one number. what is wrong i can't understand please guys help me
int n=sizeof(int)/sizeof(int);
is wrong. That just assigns 1 to n.
You mean
int n=sizeof(a)/sizeof(int);
I've not looked beyond this. No doubt there are more problems, but this is the most significant.
This is the kind of thing you can work out very easily with a debugger.
Look at this expression:
int n=sizeof(int)/sizeof(int);
What do you think the value of n is after this? (1)
Is that the appropriate value? (no, the value should be 5)
Does that explain the output you are seeing? (yes, that explains why only one number is shown)
My advice would be that if you're going to do this in C++, you actually try to use what's available in C++ to do it. I'd look up std::max_element to find the largest element in the input, and use an std::vector instead of messing with dynamic allocation directly.
When you want the number of elements in an array in C++, you might consider a function template something like this:
template <class T, size_t N>
size_t num_elements(T const (&x)[N]) {
return N;
}
Instead of dumping everything into main, I'd also write the counting sort as a separate function (or, better, a function template, but I'll leave that alone for now).
// warning: Untested code:
void counting_sort(int *input, size_t num_elements) {
size_t max = *std::max_element(input, input+num_elements);
// allocate space for the counts.
// automatically initializes contents to 0
std::vector<size_t> counts(max+1);
// do the counting sort itself.
for (int i=0; i<num_elements; i++)
++counts[input[i]];
// write out the result.
for (int i=0; i<counts.size(); i++)
// also consider `std::fill_n` here.
for (int j=0; j<counts[i]; j++)
std::cout << i << "\t";
}
int main() {
int a[]={2,3,1,2,3};
counting_sort(a, num_elements(a));
return 0;
}