#include <stdio.h>
#include <stdlib.h>
int compare(const void *a, const void *b);
int main(void) {
int input;
scanf("%d", &input);
int* array = (int *) calloc(input, sizeof(int));
for (int i = 0; i < input; i++) {
scanf("%d", &array[i]);
}
qsort(array, sizeof(array)/sizeof(int), sizeof(int), compare);
for (int i = 0; i < input; i++) printf("%d ", array[i]);
return 0;
}
int compare(const void *a, const void *b)
{
int num1 = *(int *)a;
int num2 = *(int *)b;
if (num1 < num2) return -1;
if (num1 > num2) return 1;
return 0;
}
I am still a student of C language basics. It might be a very basic question because you haven't learned it all right.
I'm trying to sort a dynamic array.
I created and sorted a dynamic array, but looking at the result, there is no sorting at all. What is the problem?
The value for nitems (second parameter) passed to qsort is wrong.
Your program works if you change the qsort call to:
qsort(array, input, sizeof(int), compare);
sizeof can't be used on malloc'd memory the way you're doing it. It only
works on fixed sized arrays like int array[10]. Because at compile time the compiler knows how many elements there are.
sizeof(array)/sizeof(int) // <= won't work. is equivalent to
sizeof(int*) / sizeof(int)
The result is probably 1. So you're qsorting the first element only
Related
For n=3 and a={1,2,3},b={4,5,6} its supposed to calculate 1*4+2*5+3*6.
I don't understand why does it work because p is a pointer and p=produs(a,b,n) means that the address of p becomes the value returned by produs.
#include <stdio.h>
#include <conio.h>
void citire(int *x,int *n)
{
for(int i=1; i<=*n; i++)
scanf("%d",&x[i]);
}
int produs(int *a,int*b,int n)
{
int produs=0;
for(int i=1;i<=n;i++)
produs=a[i]*b[i]+produs;
return produs;
}
int main()
{
int n;
int*p;
scanf("%d",&n);
int *a=(int*)malloc(n*sizeof(int));
int *b=(int*)malloc(n*sizeof(int));
citire(a,&n);
citire(b,&n);
p=produs(a,b,n);
printf("%d",p);
return 0;
}
When you do:
size_t size = 10;
int* x = calloc(size, sizeof(int));
You get an array x with 10 items in it, indexed 0..9, not 1..10. Here calloc is used to make it abundantly clear what's being requested instead of doing multiplication that can be mysterious or obtuse.
As such, to iterate:
for (int i = 0; i < size; ++i) {
x[i] ...
}
You have a number of off-by-one errors in your code due to assuming arrays are 1..N and not 0..(N-1).
Putting it all together and cleaning up your code yields:
#include <stdio.h>
#include <stdlib.h>
void citire(int *x, size_t s)
{
for(int i=0; i < s; i++)
scanf("%d", &x[i]);
}
int produs(int *a, int* b, size_t s)
{
int produs = 0;
for(int i = 0; i < s; i++)
produs = a[i] * b[i] + produs;
return produs;
}
int main()
{
int n;
scanf("%d",&n);
int* a = calloc(n, sizeof(int));
int* b = calloc(n, sizeof(int));
citire(a, n);
citire(b, n);
// produs() returns int, not int*
int p = produs(a,b,n);
printf("%d", p);
return 0;
}
You're using pointers in places where pointers don't belong. In C passing a pointer to a single value means "this is mutable", but you don't change those values, so no pointer is necessary nor advised.
Try and use size_t as the "size of thing" type. That's what's used throughout C and it's an unsigned value as negative indexes or array lengths don't make any sense.
I am trying to scanf values to an array from another function using pointer to pointer. Here's the code:
int initialize(int **arr, int count);
int main()
{
int count;
int *numbers;
scanf("Enter the amount of numbers to enter: %d", &count);
initialize(&numbers, count);
free(numbers);
return 0;
}
int initialize(int **arr, int count)
{
int i = 0;
*arr = calloc(count, sizeof(int));
while(i < count)
{
printf("Nr. %d: ", i + 1);
scanf("%d", &arr[i]);
i++;
}
return 0;
}
It allocates the memory correctly, however, there seems to be a problem inside scanf function in initialize so it crashes after reading in first 2 numbers. Could you help solve it?
arr is a pointer to pointer to int, so 1st make it a pointer to int (before using it like an array) by doing *arr.
So this
scanf("%d", &arr[i]);
should be
scanf("%d", &(*arr)[i]);
or its shorter equivalent
scanf("%d", *arr + i);
Unrelated, but in C it should be at least
int main(void)
Unrelated^2: Sizes and indexes in C best are defined using size_t (coming with stdlib.h).
So the relevant part of your code would look like this:
int main(void)
{
size_t count;
int *numbers;
scanf("Enter the amount of numbers to enter: %zu", &count);
...
int initialize(int **arr, size_t count)
{
size_t i = 0;
*arr = calloc(count, sizeof(int));
while (i < count)
...
Last not least the code misses error checking for the relevant functions:
scanf()
calloc()
Error checking (along with logging the errors) is debugging for free!
I'm newbie in C and wanted to create a function that receives one integer n and one array V, and check if my array contains the values of 1 to n.
This is my actual code:
#include <stdio.h>
void checkArray(int n, int* V);
void checkArray(int n, int* V){
int remain = n;
size_t length = sizeof V / sizeof V[0];
for(int i = 0; i<length;i++){
for(int j = 0; j<length;j++){
if(V[j] == remain){
remain--;
}
}
}
if(remain == 0)
printf("It's Latin");
printf("Not Latin");
}
int main(){
int n;
scanf("%d", &n);
int V[] = {1,2,3,4,5,6,7,8};
printf(checkArray(n, V));
}
I'm getting the error at my printf, where it says invalid use of void expression.
Your checkArray() function returns nothing (which is what void means) and you are trying to print that 'nothing' which is obviously impossible. Your checkArray() function already prints the result so you don't need to print anything in main().
It's seems though that you are missing else before printf("Not Latin") and your length variable is also incorrect. The V parameter of your checkArray() function is just a pointer (like a reference) to array and it doesn't hold any size information, you'll have to pass size separately to your function if you want to be able to traverse the array properly.
In my view, there are two mistakes in your program.
1. You didn't put "else" condition in function definition.
2. you already print something in your functions definitions so no need to use 'printf' in main() function.
you just call that function.
and put "return 0;" in last of the main function.
To put together:
#include <stdio.h>
void checkArray(int n, int* V, size_t length);
void checkArray(int n, int* V, size_t length){
int remain = n;
for(int i = 0; i<length;i++){
for(int j = 0; j<length;j++){
if(V[j] == remain){
remain--;
}
}
}
if(remain == 0){
printf("It's Latin");
else {
printf("Not Latin");
}
}
int main(){
int n;
size_t length;
scanf("%d", &n);
int V[] = {1,2,3,4,5,6,7,8};
length = sizeof(V) / sizeof(V[0]);
checkArray(n, V, length);
return 0;
}
I am trying to use the inbuilt qsort() function in C to sort an array. Below is the code I wrote
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int cmpfunc (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int main(void) {
int size,i;
printf("Enter the size of strings:\n");
scanf("%d", &size);
int a[size],identity[size];
printf("***********************************************************\n");
printf("Enter the first string:\n");
for(i=0;i<size;i++)
scanf("%d", &a[i]);
printf("Before sorting A is: \n");
for( i = 0 ; i < size; i++ ) {
printf("%d ", a[i]);
}
identity = qsort(a, size, sizeof(int), cmpfunc);
printf("\nAfter sorting the list is: \n");
for( i = 0 ; i < size; i++ ) {
printf("%d ", identity[i]);
}
return 0;
}
Since I am from Python programming practices, I am not understanding how to make the array called identity hold the value of sorted array a , which I believe is the output of qsort()
Any help/suggestion is much appreciated.
Nothing is returned from qsort, it simply sorts your array in-place. The void return type at the start of the prototype specifies lack of a return value:
void qsort(void *arr, size_t num, size_t sz, int (*fn)(const void*, const void*));
Hence you call it as:
qsort (a, size, sizeof(int), cmpfunc);
and the "return value" is the array a itself, sorted according to the parameters you passed in.
That means you have no original array at the end, unless you make a copy of it first. If you really need to have a new sorted array (it's unusual), you can use something like:
int *identity = malloc (size * sizeof(int));
if (identity == NULL)
complainBitterlyAndExit();
memcpy (identity, a, size * sizeof(int));
qsort (identity, size, sizeof(int), cmpfunc);
// Now have original a and sorted identity.
// Need to free (identity) at some point.
In default qsort function return void so you use following code
qsort(a, size, sizeof(int), cmpfunc);
printf("\nAfter sorting the list is: \n");
for( i = 0 ; i < size; i++ ) {
printf("%d ", a[i]);
}
I keep getting a segmentation fault in this code:
#include <stdio.h>
void FillArray(int *array, int);
#define MAX 256
int main()
{
int *array[MAX], size = 100;
FillArray(*array, size);
return 0;
}
void FillArray(int *array, int size)
{
int i, temp;
for (i = 0; i < size; i ++)
{
temp = (rand()%101);
*array = temp;
printf ("array[%d]. %d\n", i, *array);
array += i;
}
printf ("AJGIUEROGUSHFDJGJDFK/n");
}
I put the printf on the last line so that i could tell if it would reach that point, so far it hasn't.
Edit: I added code. I have to use pointer arithmetic instead of array indexes.
Your array in main is declared as an array of int * pointers. This array is not initialized, i.e. all elements contain garbage values.
Layer your FillArray call in main
FillArray(*array, size);
passes the value of *array to FillArray function. *array is the same as array[0] - it is an uninitialized garbage pointer that points nowhere.
Inside FillArray function you are attempting to access (and write) data through that uninitialized garbage pointer. Expectedly, the code crashes.
As is always the case with invalid code, there's no way to fix the error until you explain what you are trying to do.
I can only guess that all you needed is an array of int elements, not int * elements. I.e. your array in main was supposed to be declared as int array[MAX]. And FillArray should have been called as FillArray(array, size). Also, inside the cycle it is supposed to be array += 1 (or just ++array), not your array += i, which does not make any sense.
If wanna fill the array passed to your function, then change
array = &temp;
to
*array = temp;
And also change
array += i;
to
array++;
EDIT: OP edited his question and want to fill an array of integers. You need to chage the declaration of your array
int *array[MAX], size = 100; // Declare an array of pointers
to
int array[MAX], size = 100; // Declates an array of ints
Your loop should just be:
int i, temp;
for (i = 0; i < size; i ++)
{
temp = rand() % 101;
array[i] = temp;
printf ("array[%d] = %d\n", i, array[i]);
}
This will do what you want. There's no need to re-assign array inside the function, although you can. It's easier to just use the indexing operator []. Remember that
a[i]
is the same as
*(a + i)
regardless of the types involved (but generally a is a pointer type and i an unsigned integer) as long as the sum is a pointer of course.
There are errors in main(), too:
The array should just be int array[MAX];.
The call should just be FillArray(array, size);.
probably your want.
#include <stdio.h>
#include <stdlib.h>
void FillArray(int *array, int);
#define MAX 256
int main(){
int array[MAX], size = 100;
FillArray(array, size);
return 0;
}
void FillArray(int *array, int size){
int i;
for (i = 0; i < size; i++){
*array = rand()%101;
printf ("array[%d]. %d\n", i, *array);
++array;
}
}
int *array[MAX] is an array of MAX pointers to int, from which you pass the 1st to the function. There are no ints defined where the latter points to.
To fix this appliy the changes void FillArray(int *array, int size) provided by the other answers and then call it like this:
int main(void)
{
int array[MAX], size = 100;
FillArray(array, size);
return 0;
}
Code is fine except for your perception that *array = &array, which is wrong !!
Below points might help in understating pointers better:
*array = array[0]
array = &array[0]
*(array+i) = array[i]
Made changes to your code and it should work fine:
#include <stdio.h>
void FillArray(int *array, int);
#define MAX 256
int main()
{
int *array, size = 100;
array=(int *)calloc(MAX,sizeof(int));
if(array !=NULL)
{
FillArray(array, size); /* While calling send &array[0] but not array[0] */
}
return 0;
}
void FillArray(int *array, int size)
{
int i, temp;
for (i = 0; i < size; i ++)
{
temp = (rand()%101);
*(array+i) = temp;
printf ("array[%d]. %d\n", i, *(array+i));
/* array += i; <-- not necessary */
}
printf ("AJGIUEROGUSHFDJGJDFK/n");
}