Function to generate random ints in array doesn't work - arrays

My function does not work and I do not know why, it ends after entering the range. Could you explain why and how to fix it? I need to do this using these pointers to the array.
void generate(int *pa, int *pa2);
void display(int *pa, int *pa2);
int main()
{
srand(time(0));
int size;
printf("Enter size of array\n");
scanf("%d",&size);
int *A=(int*) malloc(size*sizeof(int));
int *ptr=A;
int *pa=&ptr[0];
int *pa2=&ptr[size-1];
generate(pa,pa2);
display(pa,pa2);
return 0;
}
void generate(int *pa, int *pa2)
{
int upper,lower;
int randi;
printf("enter range");
scanf("%d %d",&lower,&upper);
for (int i = 0; i <*pa2; i++)
{
randi=(rand() % (upper - lower + 1)) + lower;
*(pa+i) = randi;
}
}

for (int i = 0; i <*pa2; i++)
You've mixed up iterating by index with iterating until you hit a pointer. That's comparing the value of the end of the array, which is garbage, to i.
Instead you need to compare the pointers pa+1 to pa2. And it has to be <= because you do want to fill in the last item.
for (int i = 0; (pa+i) <= pa2; i++) {
*(pa+i) = ...
}
But it's easier to get rid of i and increment a pointer directly.
void generate(int *start, int *end) {
...
for(int *current = start; current <= end; current++) {
*current = ...;
}
}
But since you have the size, it's simpler to pass in the size and iterate by index.
void generate(int *array, int size) {
...
for (int i = 0; i <= size; i++) {
array[i] = ...
}
}
And you can simplify calling the function. A and &A[0] point to the same memory.
generate(A, &A[size-1]);

Related

Why is the program giving a wrong answer, when I am trying to find maximum of an array using call by reference?

I want to write a function, who gives me the maximum of an array and it should be with call by reference.
Here is my Code:
void max_array (int *array[], int len, int *max){
for (int i = 0; i < len; ++i) {
if (*max < &array[i]){
*max = array[i];
}
}
}
int main() {
void print_array (int array [], int len);
int array[] = {5,3,2,6,4,6,1};
int len = 8;
int max = 0;
max_array(array, len, &max);
printf("Max of Array: %d \n", max);
return 0;
}
Like u see there is something wrong.
My Output is like 158879987 so an address.
You are storing 8 in variable len, however your array has only 7 elements.
In the function, you have specified the first parameter incorrectly. Arrays are always passed by reference, you don't need a * and [], choose one.
void max_array (int *array, int len, int *max){
for (int i = 0; i < len; ++i) {
if (*max < array[i]){
*max = array[i];
}
}
}
remember to alter the value in the len variable.

Trying to write a program to fill an array with random numbers in C

My issue is that I am getting segmentation fault (core dumped) each time I try, I have yet to clean up my code, but I am stumped.
I must enter the values in with the compiler e.g "./filename 0 100" whereby 0 is min and 100 is max.
It must then fill the array of 10 elements with random numbers (0-100). I am so close, just can't fathom the main function.
Also, how can I print the array {0,1,2,3} in format "[0,1,2,3]" including the commas, without it looking like "[0,1,2,3, ]"
#include <stdlib.h>
#include <stdio.h>
int getRandom(int min, int max);
void fillArray(int data[], int size, int min, int max);
void printArray(int data[], int size);
int main(int argc, char *argv[]) {
int a;
int b;
if (argc>=3){
a = atoi(argv[1]);
b = atoi(argv[2]);
int arr[10];
printf("\t An array with random values from 0 to 100 \n");
fillArray(arr,10 ,a, b);
printArray(arr, 10);
} else {
printf("Incorrect number of arguments - please call with assignment min max\n");
}
return 0;
}
int getRandom(int min, int max) {
int result = 0;
int low = 0;
int high = 0;
if (min<max) {
low = min;
high = max+1;
} else {
low = max + 1;
high = min;
}
result = (rand() % (high-low)) + low;
return result;
}
void fillArray(int data[], int size, int min, int max){
int i;
for(i=min ; i < max+1; i++){
data[i] = getRandom(min,max);
}
}
void printArray(int data[], int size){
int i;
printf("[");
for(i=0; i<size; i++){
printf("%d,", data[i]);
}
printf("]");
}
I agree with #Steve Friedl that the main problem with your program lies in the fillArray function. There i should run from 0 to size.
As for your second question, testing whether you're printing the last number helps to suppress the unwanted comma:
void printArray(int data[], int size) {
printf("[");
for (int i = 0; i < size; i++) {
printf("%d", data[i]);
if (i < size - 1)
printf(",");
}
printf("]");
}
If you prefer a more compact solution (although with an optimizing compiler there's not really a difference), you could write it as:
void printArray(int data[], int size) {
printf("[");
for (int i = 0; i < size; i++) {
printf("%d%c", data[i], i < size-1 ? ',' : ']');
}
}
Also, in your main function, you should include a and b in your printing:
printf("\t An array with random values from %d to %d \n", a, b);
I believe this is blowing things up for you:
void fillArray(int data[], int size, int min, int max){
int i;
for(i=min ; i < max+1; i++){ // <-- HERE
data[i] = getRandom(min,max);
}
}
The calling function allocates 10 items in the arr array, and that's passed as the size parameter, but you're not using that parameter to limit filling up the array. If the max value is 100, then it's trying to fill one hundred slots instead of just ten.
for (i = 0; i < size; i++)
data[i] = getRandom(min,max);
should fix at least this issue.
EDIT: The comma thing, I prefer to add commas before the items unless this is the first. In this case it doesn't matter much, but it's more general, especially for variable-length lists where you don't know you're at the end until you get there. Augmenting the helpful response from #JohanC :
void printArray(int data[], int size) {
printf("[");
for (int i = 0; i < size; i++) {
if (i > 0) printf(",");
printf("%d", data[i]);
}
printf("]");
}

How to pass an array of structures to a function by reference in C?

This code is similar to what I am attempting to do, however I am getting errors saying that I am passing incompatable types
#include <stdio.h>
struct numbers{
int num;
};
void fillArray(struct numbers* a[]);
int main(void)
{
struct numbers array[4];
fillArray(&array);
for(int i = 0; i < 4; i++)
{
printf("%d", array[i].num);
}
}
void fillArray(struct numbers* a[])
{
for(int i = 0; i < 4; i++)
{
a[i]->num = i;
}
}
The function parameter
void fillArray(struct numbers* a[]);
is adjusted to
void fillArray(struct numbers ** a);
On the other hand the type of the argument in this call
fillArray(&array);
is struct numbers( * )[4]. The types struct numbers ** and struct numbers ( * )[4] are incompatible.
There is no need to pass a pointer to the array because elements of the array are already passed indirectly if you will pass just the array that is implicitly converted to pointer to its first element.
So what you need is to declare and define the function like
void fillArray( struct numbers a[] )
// or
// void fillArray( struct numbers *a )
{
for(int i = 0; i < 4; i++)
{
a[i].num = i;
}
}
and call it like
fillArray( array );
Take into account that the function depends on magic number 4. It is better to define the function such a way that it could deal with arrays of various numbers of elements.
So I would define the function like
void fillArray( struct numbers a[], size_t n )
// or
// void fillArray( struct numbers *a, size_t n )
{
for ( size_t i = 0; i < n; i++ )
{
a[i].num = i;
}
}
and call it like
fillArray( array, 4 );
Here is demonstrated how the program can look in whole
#include <stdio.h>
struct numbers
{
int num;
};
void fillArray( struct numbers a[], size_t n )
{
for ( size_t i = 0; i < n; i++ )
{
a[i].num = i;
}
}
#define N 4
int main(void)
{
struct numbers array[N];
fillArray( array, N );
for ( size_t i = 0; i < N; i++ )
{
printf( "%d ", array[i].num );
}
putchar( '\n' );
return 0;
}
Its output is
0 1 2 3
To use an array with a different number of elements it is enough to change the value of the macro name N. Thus the program and its function do not depend on the magic number 4.
You probably want this:
#include <stdio.h>
struct numbers {
int num;
};
void fillArray(struct numbers a[]);
int main(void)
{
struct numbers array[4];
fillArray(array);
for (int i = 0; i < 4; i++)
{
printf("%d\n", array[i].num);
}
}
void fillArray(struct numbers a[])
{
for (int i = 0; i < 4; i++)
{
a[i].num = i;
}
}
Your function wants an array of pointers, but you have a single array, so
void fillArray(struct numbers* a);
or
void fillArray(struct numbers a[]);
Moreover using a[i] you are already dereferencing the pointer, so you need . not -> operator.
#include <stdio.h>
struct numbers
{
int num;
};
void fillArray(struct numbers *a);
int main(void)
{
struct numbers array[4];
fillArray(array);
for (int i = 0; i < 4; i++)
{
printf("%d", array[i].num);
}
printf("\n");
}
void fillArray(struct numbers *a)
{
for (int i = 0; i < 4; i++)
{
a[i].num = i;
}
}
Finally using c array decays to pointer to first item of array, so
fillArray(&array);
must be
fillArray(array);
or
fillArray(&array[0]);
Lastly you should pass size of array to your function, instead of using fixed numbers. You can do it using pointer and size
#include <stdio.h>
struct numbers
{
int num;
};
void fillArray(size_t size, struct numbers *a);
int main(void)
{
struct numbers array[4];
fillArray(sizeof(array)/sizeof(array[0]), array);
for (int i = 0; i < 4; i++)
{
printf("%d", array[i].num);
}
printf("\n");
}
void fillArray(size_t size, struct numbers *a)
{
for (size_t i = 0; i < size; i++)
{
a[i].num = i;
}
}
Or using VLAs
#include <stdio.h>
struct numbers
{
int num;
};
void fillArray(size_t size, struct numbers a[size]);
int main(void)
{
struct numbers array[4];
fillArray(sizeof(array)/sizeof(array[0]), array);
for (int i = 0; i < 4; i++)
{
printf("%d", array[i].num);
}
printf("\n");
}
void fillArray(size_t size, struct numbers a[size])
{
for (size_t i = 0; i < size; i++)
{
a[i].num = i;
}
}
It is sufficient to only mention the array name. The compiler will transform it into a pointer to the first element.
So instead of
fillArray(&array);
you write
fillArray(array);
Though this answers your question, of course the function being called must have a compatible definition, which in your case is not so.

C Program, function sorting through pointers

This program is supposed to take an array, and sort it from lowest to highest value. My program won't sort any values though. I believe the error is in the selectionSort. The values i and j are present in the function, I printed them out inside the function but they are not passed into the swap function. I tried making i and j pointers but it didn't work. I just have no clue on what to do next. Any help would be appreciated.
#include <stdio.h>
#define N 5
void selectionSort(int *a, int n);
int *findLargest(int *a, int n);
void swap(int *p, int *q);
int main(void)
{
int i;
int a[N];
printf("Enter %d numbers: ", N);
for (i = 0; i < N; i++) {
scanf("%d", &a[i]);
}
selectionSort(a, N);
printf("In sorted order:");
for (i = 0; i < N; i++) {
printf(" %d", a[i]);
}
printf("\n");
return 0;
}
void selectionSort(int *a, int n)
{
int *p = a;
int i;
int j;
if (n == 1) {
return;
}
i = *(p+n-1);
j = *findLargest(a, n);
swap(&i, &j);
selectionSort(a, n - 1);
}
int *findLargest(int *a, int n)
{
int *p;
int *p_max = a;
for(p = a + 1; p < a + n - 1; p++) {
if ( *p > *p_max)
p_max = p;
}
return p_max;
}
void swap(int *p, int *q)
{
int temp = *(p-1);
*(p-1) = *q;
*q = temp;
}
The problem is in your call of swap: you swap the content of two local variables
int i;
int j;
... // Some other code, then
swap(&i, &j);
This has no effect on the original array. You should be passing p+n-1 and findLargest(a, n) directly, or store their results in pointers, not in ints:
swap(p+n-1, findLargest(a, n));
In addition, your swap is broken: rather than swapping the content of two pointers, it assumes that p points one element past the target location. This is a bad assumption to make in a general-purpose function, such as swap, and it also leads to undefined behavior in your program.
void swap(int *p, int *q) {
int temp = *p;
*p = *q;
*q = temp;
}

Mergesort An Array of Strings in C

I'm trying to implement a merge sort for an array of strings entered from standard input, and am at a loss at what is wrong. Right now I'm facing a segmentation fault. How should I modify my code?
main() {
char temp;
int i = 0;
char Strings[NUM][LEN];
printf("Please enter %d strings, one per line:\n", NUM);
for (i; i < 25; i++) {
fgets(&Strings[i][0], LEN, stdin);
}
i = 0;
puts("\nHere are the strings in the order you entered:");
for (i; i < 25; i++) {
printf("%s\n", Strings[i]);
}
mergesort(Strings, NUM);
i = 0;
puts("\nHere are the strings in alphabetical order");
for (i; i < 25; i++) {
printf("%s\n", Strings[i]);
}
}
int mergesort(char list[NUM][LEN], int length) { // First part
mergesort_r(0, length, list);
return 0;
}
int mergesort_r(int left, int right, char list[NUM][LEN]) { // Overloaded portion
if (right - left <= 1) {
return 0;
}
int left_start = left;
int left_end = (left + right) / 2;
int right_start = left_end;
int right_end = right;
mergesort_r( left_start, left_end, list);
mergesort_r( right_start, right_end, list);
merge(list, left_start, left_end, right_start, right_end);
}
int merge(char list[NUM][LEN], int left_start, int left_end, int right_start, int right_end) {
int left_length = left_end - left_start;
int right_length = right_end - right_start;
char *left_half[left_length];
char *right_half[right_length];
int r = 0;
int l = 0;
int i = 0;
for (i = left_start; i < left_end; i++, l++) {
strcpy(left_half[l], list[i]);
}
for (i = right_start; i < right_end; i++, r++) {
strcpy(right_half[r], list[i]);
}
for (i = left_start, r = 0, l = 0; l < left_length && r < right_length; i++) {
if (strcmp(left_half[l], right_half[r]) < 0) {
strcpy(list[i], left_half[l++]);
} else {
strcpy(list[i], right_half[r++]);
}
}
for ( ; l < left_length; i++, l++) {
strcpy(list[i], left_half[l]);
}
for ( ; r < right_length; i++, r++) {
strcpy(list[i], right_half[r]);
}
return 0;
}
I'm not sure if it's that I'm passing in my array incorrectly, or maybe it's that I am not even executing swaps properly. I'm at my wits end with this and could use some advice.
should be
char left_half[left_length][LEN];
char right_half[right_length][LEN];
#include<stdio.h>
#include<stdlib.h>
#include<string.h> //To use the string functions like strcmp and strcpy
#define MAX 10 // This is the default size of every string
void Merge(char* arr[],int low,int mid,int high) //Merging the Array Function
{
int nL= mid-low+1;
int nR= high-mid;
char** L=malloc(sizeof(char *)*nL);
char** R=malloc(sizeof(char *)*nR);
int i;
for(i=0;i<nL;i++)
{
L[i]=malloc(sizeof(arr[low+i]));
strcpy(L[i],arr[low+i]);
}
for(i=0;i<nR;i++)
{
R[i]=malloc(sizeof(arr[mid+i+1]));
strcpy(R[i],arr[mid+i+1]);
}
int j=0,k;
i=0;
k=low;
while(i<nL&&j<nR)
{
if(strcmp(L[i],R[j])<0)strcpy(arr[k++],L[i++]);
else strcpy(arr[k++],R[j++]);
}
while(i<nL)strcpy(arr[k++],L[i++]);
while(j<nR)strcpy(arr[k++],R[j++]);
}
void MergeSort(char* arr[],int low,int high) //Main MergeSort function
{
if(low<high)
{
int mid=(low+high)/2;
MergeSort(arr,low,mid);
MergeSort(arr,mid+1,high);
Merge(arr,low,mid,high);
}
}
int main()
{
printf("\nEnter the size of the array desired: ");
int size; //This is the String array size
scanf("%d",&size);
char** arr= malloc(sizeof(char *)* size); //Creating required string array
printf("\nEnter the strings of the array: ");
int i;
for(i=0;i<size;i++)
{
arr[i]=malloc(sizeof(char)*MAX);
printf("\nEnter String: ");
scanf("%s",arr[i]);
}
MergeSort(arr,0,size-1);
printf("\nThe Sorted Array is: ");
for(i=0;i<size;i++)printf("%s ->",arr[i]);
return 0;
}
This is a Working solution to the same problem. Hope it Helps!
Cheers! :)
This solution of yours might give a memory error for long inputs or repeated executions. You need to free the allocated memory or not dynamically allocate it in the first place.
The latter is an easier option. What you can do is find the length of the longest string in the array of strings before hand and pass it as an argument to the merge sort and merge function.
Let's say that length is LEN.
Then instead of dynamically allocating memory for the L and R array, just declare it as:
char L[nL][LEN] and char R[nR][LEN].
It might take a slightly larger stack memory but avoids crashing the program.

Resources