-1

I have whole program and it works but not properly. printArray function prints only first element of an Array and it doesn't seem to sort elements. I don't know how to deal with it.

int main(){
    int number;
    printf( "Give a number of elements:" );
    scanf("%d",&number);
    printf("Array to sort:");
    int* Array = createArray(number);
    int sizeArr = sizeof(Array)/sizeof(Array[0]);
    int *sortedArr = insertionSort(Array,sizeArr);
    puts("");
    printf("Sorted array by Insertion Sort:");
    printArr(sortedArr,sizeArr);
    getch();
    return 0;
}
int* createArray(int number){
    int *arr =(int*) malloc(sizeof(int*)*number);
    srand((unsigned)time(NULL));
    for(int i=0;i<number;i++){
        arr[i] = 1 + rand()%10;
        printf("%d",arr[i]);
    }
    return arr;
}
int *insertionSort(int *arr,int sizeArr){
    int i, j,repArr;
    for(i=1;i<sizeArr;i++){
            repArr = arr[i];
            j=i-1;
    while(j>=0 && arr[j]>repArr){
        arr[j+1] = arr[j];
        j = j - 1;
    }
        arr[j+1] = repArr;
    }
return arr;
}

void printArr(int*arr,int sizeArr){
    for(int i=0;i<sizeArr;i++){
        printf("%d",arr[i]);
        }
       puts("");
}
Kasia Iks
  • 3
  • 5
  • `malloc`... is it C? – YSC May 02 '19 at 13:54
  • @YSC: Seems like a mix - but the `cout< – andreee May 02 '19 at 13:56
  • Please don't forget to show us a [Minimal, **Complete**, and **Verifiable** Example](https://stackoverflow.com/help/mcve) to show us. And please take some time to read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude May 02 '19 at 13:56
  • [Here's a better insertion sort](https://stackoverflow.com/questions/24650626/how-to-implement-classic-sorting-algorithms-in-modern-c) – Caleth May 02 '19 at 14:02
  • I have changed i. Why am I not allowed to use malloc in C++? So, am I suppose to change all on C? :( I admit that I don't write in C++, but I want to learn so I hope that you will help ;) – Kasia Iks May 02 '19 at 14:05
  • `sizeof(Array)/sizeof(int)` is wrong. It should only be used on arrays, not on pointers. – interjay May 02 '19 at 14:11
  • but how replace it? I 've change it on size(Array)/sizoef(Array[0]) – Kasia Iks May 02 '19 at 14:14
  • It's not "forbidden" to use `malloc` in C++, but you shouldn't do it. First of all you should be using [`std::vector`](https://en.cppreference.com/w/cpp/container/vector), and if you have to use pointers use [smart pointers](https://en.cppreference.com/w/cpp/memory#Smart_pointers). And if you need to allocate memory yourself explicitly then use `new[]` instead of `malloc`. Also, if you want to learn C++ properly, then please [get a couple of good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to read. – Some programmer dude May 02 '19 at 14:19
  • ok, I will change a code, but there is so much errors – Kasia Iks May 02 '19 at 14:25
  • Ok, I did it , thanks for your help! – Kasia Iks May 02 '19 at 14:37

1 Answers1

1

tl;dr: It is your responsibility to know how many elements there are in an allocation. You allocated1 number elements, so the size is number.

This line does not do what you think it does. It will compare the size of int and int *. On your platform they are the same size.

int sizeArr = sizeof(Array)/sizeof(int);

You are telling all your functions that you have an array of 1 element, so you sort that 1 element, and print that 1 element.

You already know how many ints (you are pretending) exist, it is number.

Rather than interpreting the result of malloc as a bunch of ints (there are no ints there, so your program is undefined), you can use std::vector<int>, which has a size member.

Here's a more C++ approach

#include <iostream>
#include <random>
#include <vector>
#include <algorithm>

std::random_device rd;  //Will be used to obtain a seed for the random number engine
std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()

std::vector<int> createNumbers(int number){
    std::vector<int> arr(number);
    std::uniform_int_distribution<> d10(1, 10);
    std::generate_n(begin(arr), number, [&](){ return d10(gen); });
    return arr;
}

template<class FwdIt, class Compare = std::less<>>
void insertionSort(FwdIt first, FwdIt last, Compare cmp = Compare{})
{
    for (auto it = first; it != last; ++it) {
        auto const insertion = std::upper_bound(first, it, *it, cmp);
        std::rotate(insertion, it, std::next(it)); 
    }
}

template<typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec)
{
    for (auto& el : vec)
    {
        os << el << ' ';
    }
    return os;
}

int main(){
    int number;
    std::cout << "Give a number of elements:";
    std::cin >> number;
    auto Numbers = createNumbers(number);
    std::cout << "Array to sort:" << Numbers;
    insertionSort(begin(Numbers), end(Numbers));
    std::cout << "Sorted array by Insertion Sort:" << Numbers;
    return 0;
}

See it live

1: ignoring the confusion over int and int *, which are the same size on your platform. On other platforms you have some multiple of number elements.

Caleth
  • 52,200
  • 2
  • 44
  • 75