why my selection sort is not swapping values? - c

This code is not giving any syntax error but is has some logical error and it seems the program is working before swap function and the values are not swapping while running the program the output window freezes. Please help:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void swap(char *a, char *b); //function to swap strings
int main() {
char *fruits[] = { "lemon", "grapes", "water melon" };
char minstr[15];
int min_ind;
int c;
for (int i = 0; i < 3; i++) {
strcpy(minstr, fruits[i]); //copies the ith value into minstr array from fruits array
printf("%s\n", minstr);
min_ind = i;
for (int j = i + 1; j < 3; j++) {
c = (strcmp(fruits[j], minstr));
printf("value of c is %d\n", c);
if (c < 0)
min_ind = j;
printf("value of min ind %d\n",min_ind);
}
if (min_ind != i)
swap(minstr, *(fruits + i));
}
printf("the sorted aray is:\n");
for (int k = 0; k < 3; k++) {
printf("%s\n", fruits[k]);
}
return 0;
}
void swap(char *a, char *b) {
char *temp = (char *)malloc((strlen(a) + 1) * sizeof(char));
strcpy(temp, a);
` strcpy(a, b);
strcpy(b, temp);
free(temp);
}

Your code does not swap the string pointers, it attempts to swap thestring contents. This assumes both strings point to char that are large enough to accommodate the new strings and that are modifiable. This is not the case of the string literals you intialize fruits with.
You should just swap the pointers:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
const char *fruits[] = { "lemon", "grapes", "water melon" };
for (int i = 0; i < 3; i++) {
int min_ind = i;
for (int j = i + 1; j < 3; j++) {
int c = strcmp(fruits[j], fruits[i]);
printf("value of c is %d\n", c);
if (c < 0)
min_ind = j;
printf("value of min ind %d\n", min_ind);
}
if (min_ind != i) {
const char *temp = fruits[i];
fruits[i] = fruits[j];
fruits[j] = temp;
}
}
printf("the sorted array is:\n");
for (int k = 0; k < 3; k++) {
printf("%s\n", fruits[k]);
}
return 0;
}

Related

From binary to string

I have to convert the binary to string, I tried something, but it didn't work.
This is my code (I don't allowed to change the main function!) , but I can't understand what's wrong:
#include <stdio.h>
#include <stdbool.h>
#include<math.h>
#include<string.h>
void decode_bytes(const int rows, bool bytes[rows][8], char string[rows]){
int iteration = 8;
for(int j = 0; j < rows; j++){
for (int i = 0; i < iteration; i++){
if (bytes[j][i] == 1){
string[j] += pow(2, iteration - (i + 1));
}
}
}
}
int main(){
bool bytes2[7][8] = {
{0,1,0,0,1,0,0,0},
{0,1,1,0,0,1,0,1},
{0,1,1,0,1,1,0,0},
{0,1,1,0,1,1,0,0},
{0,1,1,0,1,1,1,1},
{0,0,1,0,0,0,0,1},
{0,0,0,0,0,0,0,0}
};
char string[7];
decode_bytes(7, bytes2, string);
printf("%s\n", string);
}
Output must be: "hello", but it's "cel<mB"....

How to create and return dynamic array with function parameters

I have a problem returning dynamic array pointer with function parameter. I get segfault
#include <stdio.h>
#include <stdlib.h>
void createArray(int *ptr, int n)
{
ptr = malloc(n * sizeof(int));
for(int i = 1; i <= n; ++i)
{
*(ptr + (i - 1)) = i*i;
}
}
int main() {
int *array = NULL;
int n = 5;
createArray(array, n);
for(int i = 0; i < n; ++i)
{
printf("%d", array[i]);
}
return 0;
}
I have to fill my array with i*i, when I is from 1 to n.
I don't get any errors or warnings. Just message about segmentation fault. Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
Memory must be allocate in the calling function, but not in called.
This variant works:
#include <stdio.h>
#include <stdlib.h>
void createArray(int *ptr, int n){
int i;
for(i = 1; i <= n; i++) {
*(ptr + (i - 1)) = i*i;
// fprintf(stdout,"%d %d\n", i, *(ptr + (i -1)));fflush(stdout);
}
}
int main() {
int i, n, *array = NULL;
void *pvc;
n = 5;
array = (int *)malloc(n * sizeof(int));
createArray(array, n);
for(i = 0; i < n; i++) {
fprintf(stdout,"%d %d\n", i, array[i]);fflush(stdout);
}
pvc = (void *)array;
free(pvc);
return 0;
}
You can change pointer through function parameters like this:
void createArray(int **ptr, int n)
{
*ptr = malloc(n * sizeof(int));
for(int i = 1; i <= n; ++i)
{
(*ptr)[i - 1] = i*i;
}
}
int main() {
int *array = NULL;
int n = 5;
createArray(&array, n);
Remember to call function like this: createArray(&array, n);

making this code dynamic and taking user input string for algorithm implementation

This program does not take any user input for a string
I want to take user input strings
and that to having dynamic allocation
#include <stdio.h>
#include <string.h>
void search(char* pat, char* txt)
{
int M = strlen(pat);
int N = strlen(txt);
for (int i = 0; i <= N - M; i++) {
int j;
for (j = 0; j < M; j++)
if (txt[i + j] != pat[j])
break;
if (j == M) // if pat[0...M-1] = txt[i, i+1, ...i+M-1]
printf("Pattern found at index %d \n", i);
}
}
int main()
{
char txt[] = "Rohan is a Weeb and Rohan plays sports";
char pat[] = "Rohan";
search(pat, txt);
return 0;
}
Make txt[] and pat[] a dynamic character array for a matching string algorithm program

I'm try to allocate um String matrix with malloc, but returns segmantation fault, whats happen?

I'm try to allocate an string matrix, but, in the last line mt code returns Segmentation Fault, how can i fix it?
char **allocate(char ***map, int lin, int col){
int index = lin;
map = (char*** ) malloc(sizeof(char) * lin);
for(int i = 0; i < index; i++){
map[i] = (char**) malloc(sizeof(char) * col);
}
return (char**) map;
}
void **fill(char ***map, int index){
printf("index: %d\n", index);
for(int i = 0; i <index; ++i){
for (int j = 0; j < index; ++j){
map[i][j] = "aaaaaaaaa";
printf("%s ", map[i][j]);
}
printf("\n");
}
}
int main(){
char **map = NULL;
map = allocate(map,5,5);
printf("\n");
fill(map,5);
return 0;
}
I expect only to show the last line of my matrix.
I guess you mean something like the following.
Note the "const" as a string is a "const char *" not a "char *".
#include <malloc.h>
#include <stdio.h>
const char ***allocate(int lin, int col){
int index = lin;
const char ***map = (const char*** ) malloc(sizeof(char**) * lin);
for(int i = 0; i < index; i++){
map[i] = (const char**) malloc(sizeof(char*) * col);
}
return map;
}
void **fill(const char ***map, int index){
printf("index: %d\n", index);
for(int i = 0; i < index; ++i){
for (int j = 0; j < index; ++j){
map[i][j] = "aaaaaaaaa";
printf("%s ", map[i][j]);
}
printf("\n");
}
}
int main(){
const char ***map = allocate(5,5);
printf("\n");
fill(map,5);
return 0;
}
Calling malloc this many times and having such a high level of indirection
makes for very inefficient coding.
It is sufficient in this case to make a constant 5x5 array of const char *
int main() {
const char *map[5][5];
for (int i = 0; i != 5; ++i) {
for (int j = 0; j != 5; ++j) {
map[i][j] = "I should write my own assignment.";
}
}
}

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