Sorting an Array with Selection Sort - c

#include<stdio.h>
#include <time.h>
int selectionSort(int data[], int count)
{
int a,b;
int tmp;
int minimum;
for(a=0; a<count-1;a++){
minimum = a;
for(b=a+1;b<count;b++){
if(data[minimum]>data[b]) {
minimum = b;
}
}
tmp = data[a];
data[a]=data[minimum];
data[minimum]=tmp;
}
return 0;
}
int main(){
int randomarray[10];
int i;
int k;
srand(time(NULL));
for(i=1; i<=10; i++){
randomarray[i]=rand()%100;
selectionSort(randomarray[i], k = 10 );
printf("%d\n",randomarray[i]);
}
return 0;
}
I am trying to create a program that sorts an array of random integers. I think there is a problem with the declaration of my function, but I am not used to using C. The errors are shown below:
semihosting_example.c:13:5: warning: implicit declaration of function 'srand' [-Wimplicit-function-declaration]
semihosting_example.c:15:5: warning: implicit declaration of function 'rand' [-Wimplicit-function-declaration]
semihosting_example.c:16:5: warning: passing argument 1 of 'selectionSort' from incompatible pointer type [enabled by default]
semihosting_example.c:6:6: note: expected 'int *' but argument is of type 'int (*)[10]'ing_example.c:6:6: note: expected 'int *' but argument is of type 'int (*)[10]

Here's the updated code with the appropriate HEADERS. I am trying to create a function with an array that calls the function with an array:
#include<stdio.h>
#include <time.h>
#include<stdlib.h>
int selectionSort(int (*)[10], int count)
{
int a,b;
int tmp;
int minimum;
for(a=0; a<count-1;a++){
minimum = a;
for(b=a+1;b<count;b++){
if(data[minimum]>data[b]) {
minimum = b;
}
}
tmp = data[a];
data[a]=data[minimum];
data[minimum]=tmp;
}
return 0;
}
int main(){
int randomarray[10];
int i;
int k;
srand(time(NULL));
for(i=1; i<=10; i++){
randomarray[i]=rand()%100;
selectionSort(randomarray[i], k = 10 );
printf("%d\n",randomarray[i]);
}
return 0;
}

Here's a revised version of your program:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
static void selectionSort(int data[], int count)
{
for (int a = 0; a < count - 1; a++)
{
int minimum = a;
for (int b = a + 1; b < count; b++)
{
if (data[minimum] > data[b])
minimum = b;
}
int tmp = data[a];
data[a] = data[minimum];
data[minimum] = tmp;
}
}
int main(void)
{
int randomarray[10];
srand(time(NULL));
for (int i = 0; i < 10; i++)
randomarray[i] = rand() % 100;
printf("Before:\n");
for (int i = 0; i < 10; i++)
printf("%d: %d\n", i, randomarray[i]);
selectionSort(randomarray, 10);
printf("After:\n");
for (int i = 0; i < 10; i++)
printf("%d: %d\n", i, randomarray[i]);
return 0;
}
When run, it produces output such as:
Before:
0: 73
1: 63
2: 73
3: 80
4: 28
5: 19
6: 63
7: 96
8: 82
9: 46
After:
0: 19
1: 28
2: 46
3: 63
4: 63
5: 73
6: 73
7: 80
8: 82
9: 96
Note that it prints the array before sorting as well as afterwards so that you can tell whether it sorted correctly.
The code doesn't use k; it declares variables as late as possible.
If you replace the line:
selectionSort(randomarray, 10);
with:
selectionSort(&randomarray, 10);
you get the error messages:
$ gcc -O3 -g -std=c11 -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes \
> sort53.c -o sort53
sort53.c: In function ‘main’:
sort53.c:32:19: error: passing argument 1 of ‘selectionSort’ from incompatible pointer type [-Werror=incompatible-pointer-types]
32 | selectionSort(&randomarray, 10);
| ^~~~~~~~~~~~
| |
| int (*)[10]
sort53.c:5:31: note: expected ‘int *’ but argument is of type ‘int (*)[10]’
5 | static void selectionSort(int data[], int count)
| ~~~~^~~~~~
$
That's from GCC 10.1.0, but it is very similar to the error messages posted in the question. If you instead replace the call with:
selectionSort(randomarray[0], 10);
then the error messages are:
sort53.c: In function ‘main’:
sort53.c:32:30: error: passing argument 1 of ‘selectionSort’ makes pointer from integer without a cast [-Werror=int-conversion]
32 | selectionSort(randomarray[0], 10);
| ~~~~~~~~~~~^~~
| |
| int
sort53.c:5:31: note: expected ‘int *’ but argument is of type ‘int’
5 | static void selectionSort(int data[], int count)
| ~~~~^~~~~~
cc1: all warnings being treated as errors

Related

Implicit declaration C

I wish to get my code cleaner, the code can compile, but unfortunately there are still some
stuff showing minor problem about this following "error message"
how can I solve this ?
#include <stdlib.h>
#include <stdio.h>
int main(){
int arr0[] = {1,2,3,4,5};
int arr1[] = {2,2,2,2,2};
int arr2[] = {1,4,2,4,4};
int sizeArr0 = sizeof(arr0);
int sizeArr1 = sizeof(arr1);
int sizeArr2 = sizeof(arr2);
parseArray(arr0[0], sizeArr0);
parseArray(arr1[0], sizeArr1);
parseArray(arr2[0], sizeArr2);
}
int parseArray(int ch[], int sizeValue){
int sum;
for(int x = 0; x < ch; x++){
int ch[x];
if(x == 5){
sum += 5;
}
if (sum == 15){
return sum;
}
}
}
warning: implicit declaration of function ‘parseArray’ [-Wimplicit-function-declaration]
17 | parseArray(arr0[0], sizeArr0);
| ^~~~~~~~~~
test.c: In function ‘parseArray’:
test.c:30:22: warning: comparison between pointer and integer
30 | for(int x = 0; x < ch; x++){
You need to have a function definition or prototype before the function which calls it
int parseArray(int ch[], size_t sizeValue);
int main()
{
/* ... */
for(int x = 0; x < ch; x++){ makes no sense and I believe that an typo.
for(size_t x = 0; x < sizeValue; x++){
int sizeArr0 = sizeof(arr0); is giving you the size of the array in char not in element types. You need to divide it by the size of the elements. It should also have different type (size_t) size_t sizeArr0 = sizeof(arr0) / sizeof(arr0[0]);
All local function variables have to be initialized as they are not zeroed as global variables. int sum = 0;
You pass the first element to the array not the reference to the array parseArray(arr0, sizeArr0); or parseArray(&arr0[0], sizeArr0);

how to find out if a serie is sorted using pointers in c

i have the following program in c lanquage:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
bool is_sorted ( int *array , int num , int prev , int *index);
int main ()
{
int N;
int i;
int prev;
int data[100];
bool flag;
printf("Enter length:\n");
scanf("%d",&N);
printf("Enter %d integers:\n" ,N);
for (i =0; i<N; i++)
{
scanf("%d",&data[i]);
}
printf("Enter previous number:\n");
scanf("%d",&prev);
int *index= NULL;
flag = is_sorted(data,N,prev,index);
if ( !flag )
{
printf("%d ", *index);
}
}
bool is_sorted ( int *array , int num , int prev , int *index)
{
if ( prev > array[0] )
{
index=prev;
return false;
}
for ( int i=0; i<num; i++)
{
if ( array[i] > array[i+1] )
{
index = array[i];
return false;
}
}
return true;
}
The function is_sorted takes as input an array of integers and another one random integer and returns true if prev < array[0] < array[1] < ... < array[n].
I am using a pointer in order to find which is the first element to spoil the serie's order but i am a little bit confused with pointer's syntax.
Running it i am getting the following results:
pointers.c:43:14: warning: assignment to ‘int *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
43 | index=prev;
| ^
pointers.c:51:19: warning: assignment to ‘int *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
51 | index = array[i];
index is a pointer to an integer. You need to dereference the pointer to assign to the variable that it points to in the caller.
*index = prev;
...
*index = array[i];

My Producer/Consumer C-Code is not giving any real output

I've recently changed my OS from Windows to NitruxOS, and I'm currently struggling a bit with compiling my code.
I tried compiling with:
gcc main.c -Wall -pedantic -std=c99 -D_XOPEN_SOURCE=700 -pthread
but I'm not getting any real output; only the following warning:
*main.c: In function ‘main’: main.c:29:44: warning: ISO C forbids passing argument 3 of ‘pthread_create’ between function pointer and ‘void *’ [-Wpedantic] 29 | pthread_create(&producer[i], NULL, (void*)producer, (void*)&num[i]);
| ^~~~~~~~~~~~~~~ In file included from main.c:4: /usr/include/pthread.h:200:15: note: expected ‘void * (*)(void *)’ but argument is of type ‘void *’ 200 | void *(*__start_routine) (void *),
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~ main.c:34:44: warning: ISO C forbids passing argument 3 of ‘pthread_create’ between function pointer and ‘void *’ [-Wpedantic] 34 | pthread_create(&consumer[i], NULL, (void*)consumer, (void *)&num[i]);
| ^~~~~~~~~~~~~~~ In file included from main.c:4: /usr/include/pthread.h:200:15: note: expected ‘void * (*)(void *)’ but argument is of type ‘void *’ 200 | void *(*__start_routine) (void *),*
My code is as follows:
#include <stdlib.h>
#include <stdio.h>
#include <semaphore.h>
#include <pthread.h>
#define max 10
#define bufferlen 10
sem_t voll;
sem_t leer;
int input = 0;
int output = 0;
int buffer[bufferlen];
pthread_mutex_t mutex;
int main()
{
pthread_t producer[bufferlen], consumer[bufferlen];
pthread_mutex_init(&mutex, NULL);
sem_init(&voll, 0, 0);
sem_init(&leer, 0, bufferlen);
int num[max] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for(int i = 0; i < 10; i++)
{
pthread_create(&producer[i], NULL, (void*)producer, (void*)&num[i]);
}
for(int i = 0; i < 10; i++)
{
pthread_create(&consumer[i], NULL, (void*)consumer, (void *)&num[i]);
}
for(int i = 0; i < 10; i++)
{
pthread_join(producer[i], NULL);
}
for(int i = 0; i < 10; i++)
{
pthread_join(consumer[i], NULL);
}
pthread_mutex_destroy(&mutex);
sem_destroy(&leer);
sem_destroy(&voll);
return 0;
}
void *producer(void *pptr)
{
int item = 0;
for (int i = 0; i < max; i++)
{
sem_wait(&leer);
pthread_mutex_lock(&mutex);
buffer[input] = item;
printf("Der Producer No. %d hat %d an Stelle %d eingefügt\n", *((int*)pptr), buffer[input], input);
input = (input + 1) % bufferlen;
pthread_mutex_unlock(&mutex);
sem_post(&voll);
}
return NULL;
}
void *consumer(void *cptr)
{
for(int i = 0; i < max; i++)
{
sem_wait(&voll);
pthread_mutex_lock(&mutex);
int item = buffer[output];
printf("Der Consumer No. %d hat %d von Stelle %d entfernt\n", *((int*)cptr), item, output);
output = (output + 1) % bufferlen;
pthread_mutex_unlock(&mutex);
sem_post(&leer);
}
return NULL;
}

Pointer to Array in function call in c

i'm a bit confused about using pointers.
I want to hand over the array to the pointer and then get the result of the function from the pointer.
The function just returns the minimum of an array.
int *minimum(int (*A)[5]){
int min = 0;
int A_result[1];
for (int i = 0; i < 5; i++) {
if ((*A)[i]< (*A)[min])
min = i;
}
A_result[0] = (*A)[min];
return A_result; //line 39
}
int main(void) {
int A[5] = {5, 7, 3, 6, 4};
int (*array_ptr)[5] = minimum(&A); //line 45
printf("%d ", *array_ptr);
return EXIT_SUCCESS;
}
by now i get the following errors:
line 45: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
initialization from incompatible pointer type [-Wincompatible-pointer-types]
line 39:
function returns address of local variable [-Wreturn-local-addr]
There is no need to pass pointer to array to the function second_lowest. Also, no point in returning a pointer from the function second_lowest. I modified your code :
int second_lowest(int *A) {
int min = 0;
int A_result;
for (int i = 0; i < 5; i++) {
if (A[i] < A[min])
min = i;
}
A_result = A[min];
return A_result; //line 39
}
int main(void) {
int A[5] = { 5, 7, 3, 6, 4 };
int res = second_lowest(A); //line 45
printf("%d ", res);
return EXIT_SUCCESS;
}
Hope it helps.

any stronger test case to check code

this code is getting a wrong answer in spoj
i want stronger test cases to check my code.. help appreciated..
The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how many quadruplet (a, b, c, d ) belongs to A x B x C x D are such that a + b + c + d = 0 . In the following, we assume that all lists have the same size n
Input
The first line of the input file contains the size of the lists n (this value can be as large as 4000). We then have n lines containing four integer values (with absolute value as large as 2^28 ) that belong respectively to A, B, C and D .
Example
Input:
6
-45 22 42 -16
-41 -27 56 30
-36 53 -37 77
-36 30 -75 -46
26 -38 -10 62
-32 -54 -6 45
Output:
5
#include<stdio.h>
#include<stdlib.h>
int binarys(int a,long int b[],int low,int up,int k)
{
int par;
if(low <= up) {
par = partition(low,up,b);
if(b[par] + a == 0) {
k = k + 1;
k = binarys(a,b,low,par-1,k);
k = binarys(a,b,par+1,up,k);
}
if(b[par] > -1*a)
k = binarys(a,b,low,par-1,k);
if(b[par] < -1*a)
k = binarys(a,b,par+1,up,k);
}
return k;
}
int partition(int low,int up,int b[])
{
int i;
int j;
int m;
int a;
j = low - 1;
m = b[up];
for(i=low; i < up; i++) {
if(b[i] <= m ) {
j++;
a = b[i];
b[i] = b[j];
b[j] = a;
}
}
a = b[j+1];
b[j+1] = b[up];
b[up] = a;
return j + 1;
}
int main()
{
long int *A = NULL;
long int *B = NULL;
long int *C = NULL;
long int *D = NULL;
long int *a = NULL;
long int *b = NULL;
int n;
int i;
int j;
int k;
int l;
int sum;
scanf("%d",&n);
k = 0;
sum = 0;
A = (long int*)malloc(n*sizeof(long int));
B = (long int*)malloc(n*sizeof(long int));
C = (long int*)malloc(n*sizeof(long int));
D = (long int*)malloc(n*sizeof(long int));
a = (long int*)malloc(n*n*sizeof(long int));
b = (long int*)malloc(n*n*sizeof(long int));
for(i=0; i < n; i++) {
scanf("%d%d%d%d",&A[i],&B[i],&C[i],&D[i]);
}
for(i=0; i < n; i++) {
for(j=0; j < n; j++) {
a[k] = A[i] + B[j];
b[k] = C[i] + D[j];
k++;
}
}
for(i=0; i < n*n; i++) {
l = binarys(a[i],b,0,n*n,0);
sum = sum + l;
}
printf("%d",sum);
return 0;
}
Compiling with gcc 4.7.2, -Wall -Wextra -Wshadow
a.c: In function ‘binarys’:
a.c:9:9: warning: implicit declaration of function ‘partition’ [-Wimplicit-function-declaration]
a.c: In function ‘main’:
a.c:75:9: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘long int *’ [-Wformat]
a.c:75:9: warning: format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘long int *’ [-Wformat]
a.c:75:9: warning: format ‘%d’ expects argument of type ‘int *’, but argument 4 has type ‘long int *’ [-Wformat]
a.c:75:9: warning: format ‘%d’ expects argument of type ‘int *’, but argument 5 has type ‘long int *’ [-Wformat]
Your example test case
$ ./a
6
-45 22 42 -16
-41 -27 56 30
-36 53 -37 77
-36 30 -75 -46
26 -38 -10 62
-32 -54 -6 45
0$
It neither matches the expectation you provided (5), nor does it print a newline after the output. The grader may be expecting the newline and rejecting your program simply by you not writing it.
Other basic test cases
$ ./a
1
0 0 0 0
2
Expected 1.
$ ./a
2
1 1 1 1
-3 100 100 100
0
Expected 1.
$ ./a
2
-1 2 2 2
-2 3 3 3
2
Expected 0.
$ ./a
1
9223372036854775807 -9223372036854775808 1 0 // LONG_MAX, LONG_MIN on my architecture
0
Expected 1.
$ ./a
0
0
Correct answer 0!

Resources