Mergesort An Array of Strings in C - 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.

Related

printTriangle with alphabets

#include <stdio.h>
int inputNumber();
void printTriangle(int size, char ch[]);
int main()
{
char arr_char[] = {'a','e','i','o','u'};
int number;
number = inputNumber();
printTriangle(number,arr_char);
return 0;
}
int inputNumber()
{
int size;
printf("Input Size: ");
scanf("%d",&size);
printf("===\nTriangle Size is %d\n===\n",size);
return size;
}
void printTriangle(int size, char ch[5])
{
int i,j;
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
if(j <= 4)
{
printf("%s ",ch[j]);
}
if(j > 4 )
{
printf("# ");
}
}
}
}
This is what it supposed to look like. And I'm confused as what I did wrong because it only work with inputNumber but not with printTriangle. a e i o u # # # please go easy on me since I'm kinda new to this.
The segmentation fault is caused by an incorrect format string (%s should be %c). The output format doesn't match expectations as the inner loop condition is wrong and you need a newline after you print each line (after inner loop):
#include <stdio.h>
#define LEN 5
int inputNumber() {
int size;
printf("Input Size: ");
scanf("%d",&size);
printf("===\nTriangle Size is %d\n===\n",size);
return size;
}
void printTriangle(int size, char ch[]) {
for(int i = 0; i < size; i++) {
for(int j = 0; j <= i; j++) {
printf("%c ", j < LEN ? ch[j] : '#');
}
printf("\n");
}
}
int main() {
char arr_char[LEN] = {'a','e','i','o','u'};
int number;
number = inputNumber();
printTriangle(number,arr_char);
return 0;
}
Consider using the type unsigned instead of int as negative size, row (i) and column (j) doesn't make sense.
Here is an alternatively implementation of printTriangle() that does a bit of work upfront to generate the last row, then use a prefix of that last row to print all rows:
void printTriangle(unsigned size, char ch[]) {
// use malloc if size is large (say >4k),
// or you don't want to use a variable length array (VLA)
char str[2 * size];
for(unsigned column = 0; column < size; column++) {
str[2 * column] = column < LEN ? ch[column] : '#';
str[2 * column + 1] = ' ';
}
for(unsigned row = 0; row < size; row++) {
printf("%.*s\n", 2 * row + 1, str);
}
}

reversing a double array with a recursive function in c without loop in the recursive part of the code

I've been trying to reverse a double array with a recursive function without using loops in the recursive part of the code.
I've written the code for integers and it works for all cases, however, I couldn't reverse the elements of the array as doubles
Is there anyone would like to comment on this at least to give a perception?
Thanks
#include <stdio.h>
#include <stdlib.h>
void ArrayReverse(int x[], int N)
{
if (N <= 1)
{
return;
}
else
{
int temp;
int j = 0;
temp = x[j];
x[j] = x[N-1];
x[N-1] = temp;
ArrayReverse(&x[1],N-2);
}
}
int main(int argc, char *argv[]) {
int num_of_el;
int i;
printf("enter the number of elements of the array: \n");
scanf("%d", &num_of_el);
int arr[num_of_el];
int element;
int k,l;
printf("enter the elements one by one: \n");
for(i=0;i <= num_of_el - 1; i++)
{
scanf("%d", &element);
arr[i] = element;
}
for(l=0;l <= num_of_el - 1; l++)
{
printf("%d", arr[l]);
}
printf("\n");
ArrayReverse(arr,num_of_el);
for(k=0;k <= num_of_el - 1; k++)
{
printf("%d", arr[k]);
}
return 0;
}

getting weird output from 2 d array in C

I want to a fill a small 2 d array with arbitrary values before moving onto a bigger array. However, when I compile and run my program, I get some weird output. It is not perfectly square. if someone could point out what im doing wrong that would be great.
void startarray(char (*arr)[10], int y_length, int x_length);
void printarray(char (*arr)[10], int y_length);
int main()
{
char arr[10][10];
startarray(arr, 10,10);
printarray(arr, 10);
return 0;
}
void startarray(char (*arr)[10], int y_length, int x_length)
{
int i;
int j;
for(i = 0; i <= y_length; i++)
{
for(j = 0; j < x_length; j++)
{
arr[i][j] = 'a';
}//end for
arr[i][j] = '\0';
}//end for
}
void printarray(char (*arr)[10], int y_length)
{
int i = 0;
while(i < y_length)
{
printf("\n%s", arr[i]);
i++;
}//end while
}

Printing only unique elements in an array

Edit: I need to maintain the order in which the elements are present in the original array, so sorting won't work.
I have a 1-D array containing some elements and I am using printf() in C but I only want to print an element if and only if it has not already been printed before.
I am thinking of using nested loops to compare if the element I am about to print from the current position in the array was already present in a lower index of the array but it's not working. What am I missing? Or is my whole approach wrong?
So far, I have tried this, which is not working:
int arr[20];
After this I take user input for no. of elements in p and of course, p<20. Then, user enters the elements one by one. I use scanf() for this.
for(i=1;i<=p;i++)
{
for(j=i+1;j<=p;j++)
{
if(arr[i]!=arr[j])
{
printf("%d",arr[j]);
}
}
}
You need to check all previous items before you know if the item has already occurred.
#include <stdio.h>
#include <stdlib.h>
int compareFunc(const void *op1, const void *op2 )
{
int *a, *b;
a = (int*)op1;
b = (int*)op2;
return *a - *b;
}
void printUnique(int *array, int numElems)
{
int curPrintIndex, curCompareIndex;
char alreadySeen;
for (curPrintIndex=0; curPrintIndex<numElems; curPrintIndex++)
{
alreadySeen = 0;
for (curCompareIndex=0; curCompareIndex<curPrintIndex; curCompareIndex++)
{
if (array[curCompareIndex] == array[curPrintIndex])
{
alreadySeen = 1;
break;
}
}
if (alreadySeen == 0)
printf("%d\n", array[curPrintIndex]);
}
}
int main()
{
const int numItems = 100;
int *array, i, lastVal;
array = calloc(numItems, sizeof(int) );
for (i=0; i<numItems; i++)
array[i] = rand()%numItems;
printUnique(array, numItems);
free(array);
return 0;
/*
qsort(array, numItems, sizeof(int), compareFunc);
printf("%d\n", array[0]);
lastVal = array[0];
for (i=1; i<numItems; i++)
{
if (array[i] != lastVal)
{
lastVal = array[i];
printf("%d\n", array[i]);
}
}
*/
}
#include <stdio.h>
int main(void){
int arr[20] = { 4,8,4,2,4,8,1,3,2,7 };//the elements are positive integers.
int i, j, p = 10;
for(i=0;i<p-1;i++){
if(arr[i] < 0)
continue;
for(j=i+1;j<p;j++){
if(arr[j] > 0 && arr[i]==arr[j])
arr[j] *= -1;
}
}
for(i=0; i < p; ++i)
if(arr[i] > 0)
printf("%d ", arr[i]);
puts("");
return 0;
}

equating addresses of a 2d array and a 1d array in c

This prog is to accept an array of chars n compress them....(aaaabbbcc-->a4b3c2)....my prog is showing error at the point where im equating the addr of the 2d array to 1d array. This is my code:
/* size1 defined as 5 and size2 as 10.... (consts)*/
void compress(char data[SIZE1][SIZE2]);
int main()
{
char data[SIZE1][SIZE2];
printf("Enter a 5x10 matrix of characters:\n");
scanf("%c", &data);
compress(data[SIZE1][SIZE2]);
_getch();
return 0;
}
void compress(char data[SIZE1][SIZE2])
{
int hold[SIZE1*SIZE2];
int cnt = 0;
hold[SIZE1*SIZE2] = data[SIZE1][SIZE2];
for (int i = 0; i < (SIZE1*SIZE2); i++)
{
if (hold[i] == hold[i + 1])
{
cnt++;
continue;
}
else
{
printf("%c%d", hold[i], cnt);
}
}
}
This didn't work so I tried to use pointers:
void compress(char data[SIZE1][SIZE2])
{
int *hold[SIZE1*SIZE2];
int cnt = 0;
hold = data[SIZE1][SIZE2];
for (int i = 0; i < (SIZE1*SIZE2); i++)
{
if (*(hold+i) == *(hold+i+1))
{
cnt++;
}
else
{
printf("%c%d", *(hold+i), cnt);
}
}
}
I thought that the addrs of 2d arrays are stored linearly, hence they can be directly =to that of 1d.But the error says "'=':left operand must be an l-value".Im very new to pointers.Any help or corrections ....pls?
#include <stdio.h>
#define SIZE1 3
#define SIZE2 3
void compress(char data[SIZE1][SIZE2]);
int main(){
char data[SIZE1][SIZE2];
printf("Enter a %dx%d matrix of characters:\n", SIZE1, SIZE2);
for(int i=0;i<SIZE1;++i)
for(int j=0;j<SIZE2;++j)
scanf("%c", &data[i][j]);//aaaabbbcc
compress(data);
(void)getchar();
return 0;
}
void compress(char data[SIZE1][SIZE2]){
char *hold = &data[0][0];
int cnt = 1, size = SIZE1*SIZE2;
for (int i = 0; i < size; i++){
if (i < size -1 && hold[i] == hold[i + 1]){
cnt++;
//continue;
} else {
printf("%c%d", hold[i], cnt);//a4b3c2
cnt = 1;
}
}
}

Resources