I am trying to use a bubble sort function with the swap function on an array of character as shown below:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
inline void SWAP(char * a, int j, int N)
{
if (j!=N) {a[j] ^= a[N]; a[N] ^= a[j]; a[j] ^= a[N];}
}
void bubblesort(char * a, int N)
{
int i, j;
for (i=N-1; i>0; i--)
for(j=0; j<i; j++)
if (!(a[j] <= a[j+1]))
SWAP(a,j,j+1);
}
int main()
{
int i;
int N = 0;
char * a;
a = (char *)malloc(N * sizeof(int));
printf("Enter size of array:");
scanf("%d",&N);
for(i=0; i<N; i++)
{
printf("%d. Enter your characters:", i+1);
scanf("%c\n", &a[i]);
}
bubblesort(a, N);
for(i=0;i<N;i++)
{
printf("%c\n", a[i]);
}
printf("\n");
return 0;
}
But when I run this in Linus, it works but the bubblesort with swap function only applies to the first two character elements of the array and the last element is left out. It seems the last element of the array is not stored in the array. Can any one help in fixing this?
Related
I wrote a program to get an array which will return an array with elements as difference between max value and remaining elements.
#include <stdio.h>
void behind(int *, int);
int main(void) {
int array[10];
int N, i;
scanf("%d", &N);
for (i=0; i<N; i++) {
scanf("%d", &array[i]);
}
behind(array, N);
for (i=0; i<N; i++) {
printf("%d\n", array[i]);
}
return 0;
}
void behind(int *ptr,int size) /* Write your function behind() here: */
{
int i,max=0;
for(i=1;i<size;i++){
if(ptr[i-1]>=ptr[i])
max=ptr[i-1];
else
max = ptr[i];
}
for(i=0;i<size;i++);
ptr[i]=max-ptr[i];
}
why my program always return the same array that I got as input?
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;
}
I feel like I've attempted every combination I know of to get this to work and can't figure it out. How can I scanf() into an int** passed as a pointer to a function? I tried searching but couldn't find this, if it's a duplicate please let me know and I'll delete. It begins to run and after entering a few values it segfaults.
Here's my code, I think it's messing up on the scanf() line of the setMatrix() function:
#include <stdio.h>
#include <stdlib.h>
// create zero initialized matrix
int** callocMatrix(int rmax, int colmax) {
int **mat = calloc(rmax, sizeof(int*));
for(int i = 0; i < rmax; i++) mat[i] = calloc(colmax, sizeof(int));
return mat;
}
// fill matrix
void setMatrix(int ***mat, int r, int c){
printf("Insert the elements of your matrix:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
printf("Insert element [%d][%d]: ", i, j);
scanf("%d", mat[i][j]); // problem here??
printf("matrix[%d][%d]: %d\n", i, j, (*mat)[i][j]);
}
}
return;
}
// print matrix
void printMatrix(int ***mat, int r, int c){
for (int i=0; i<r;i++){
for (int j=0; j<c;j++) {
printf("%d ", (*mat)[i][j]);
}
printf("\n");
}
}
int main(int argc, char *argv[]) {
int r = 3, c = 3;
int **mat = callocMatrix(r, c);
setMatrix(&mat, r, c);
printMatrix(&mat, r, c);
}
There is no need to use triple pointer ***. Passing two-dimensional array will work as is. Here is the code:
#include <stdio.h>
#include <stdlib.h>
// create zero initialized matrix
int** callocMatrix(int rmax, int colmax) {
int **mat = calloc(rmax, sizeof(int*));
for(int i = 0; i < rmax; i++) mat[i] = calloc(colmax, sizeof(int));
return mat;
}
// fill matrix
void setMatrix(int **mat, int r, int c){
printf("Insert the elements of your matrix:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
printf("Insert element [%d][%d]: ", i, j);
scanf("%d", &mat[i][j]); // no problem here
printf("matrix[%d][%d]: %d\n", i, j, mat[i][j]);
}
}
}
// print matrix
void printMatrix(int **mat, int r, int c){
for (int i=0; i<r;i++){
for (int j=0; j<c;j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
}
int main(int argc, char *argv[]) {
int r = 3, c = 3;
int **mat = callocMatrix(r, c);
setMatrix(mat, r, c);
printMatrix(mat, r, c);
}
Should be:
scanf("%d", &(*mat)[i][j]);
You're passing a pointer to you matrix object, so you need to dereference it (with *) just as you do with printf. scanf then needs the address of the element to write into, so you need the &
I have implemented a quicksort algorithm in C to sort the elements of an array. It works for all cases except when an the array has two or more equal elements. I've been trying to fix it and have been debugging it but I can't seem to manage to get it to work when there are repeated elements.
I'd appreciate any assistance on how I can change my code to work for repeated elements too.
#include <stdio.h>
#include <stdlib.h>
//Random Array Length
#define L 10
#define MAX 100
void smarter_sort(int[],int,int);
void swap(int[],int,int);
int choose_piv(int[],int,int);
int main(){
int i, a[L];
//Generate an array of random numbers
for(i=0; i<L; i++)
a[i]= rand() % (MAX+1);
//Unsorted Array
printf("\nUnsorted array: ");
for(i=0; i<L; i++)
printf("%d ", a[i]);
//Sorted Array
smarter_sort(a,0,L-1);
printf("\nSorted array: ");
for(i=0; i<L; i++)
printf("%d ", a[i]);
return 0;
}
//Recursively defined quicksort (Pseudo-code listing 1.9)
void smarter_sort(int a[], int l, int r){
if(r > l){
int piv = choose_piv(a, l, r);
smarter_sort(a, l, piv-1);
smarter_sort(a, piv+1, r);
}
}
//Swap Elements
void swap(int a[], int i, int j){
int t=a[i];
a[i]=a[j];
a[j]=t;
}
//Choosing the pivot (pseudo-code listing 1.10)
int choose_piv(int a[], int l, int r){
//defining pointers and pivot
int pL = l, pR = r;
int piv = l;
while (pL < pR){
//finding the first left element greater than piv
while(a[pL] < a[piv])
pL++;
//finding the first right element greater than piv
while(a[pR] > a[piv])
pR--;
//swapping if the pointers do not overlap
if(pL < pR)
swap(a, pL, pR);
if(a[pL]==a[piv]||a[pR]==a[piv]){
pL++;
pR--;
}
}
//swapping and returning the rightmost pointer as the pivot
swap(a, piv, pR);
return pR;
}
This is your code modified in order to work properly even with array containing equal elements:
#include <stdio.h>
#include <stdlib.h>
//Random Array Length
#define L 10
#define MAX 100
void smarter_sort(int[],int,int);
void swap(int[],int,int);
int main(){
int i, a[L];
//Generate an array of random numbers
for(i=0; i<L; i++)
a[i]= rand() % (MAX+1);
//Unsorted Array
printf("\nUnsorted array: ");
for(i=0; i<L; i++)
printf("%d ", a[i]);
//Sorted Array
smarter_sort(a,0,L-1);
printf("\nSorted array: ");
for(i=0; i<L; i++)
printf("%d ", a[i]);
return 0;
}
//Recursively defined quicksort (Pseudo-code listing 1.9)
void smarter_sort(int arr[], int left, int right) {
int i = left, j = right;
int pivot = arr[(left + right) / 2];
while (i <= j) {
while (arr[i] <pivot)
i++;
while (arr[j]>pivot)
j--;
if (i <= j) {
swap(arr,i,j);
i++;
j--;
}
};
if (left < j)
smarter_sort(arr, left, j);
if (i < right)
smarter_sort(arr, i, right);
}
//Swap Elements
void swap(int a[], int i, int j){
int t=a[i];
a[i]=a[j];
a[j]=t;
}
There are two cods that I have written. One works while the other wont. Please explain the working of this code wand why the latter isnt working.
Working -
#include <stdio.h>
#include <malloc.h>
int main(){
int m, n, i, j;
scanf("%d%d",&m,&n);
int *p;
p = (int *) malloc(m*n*sizeof(int));
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d", (p+i*n+j));
}
}
}
Not Working -
#include <stdio.h>
#include <malloc.h>
int main(){
int m, n, i, j;
scanf("%d%d",&m,&n);
int *p;
p = (int *) malloc(m*n*sizeof(int));
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d", p[i][j]);
}
}
}
error is - subscripted value is neither array nor pointer nor vector
scanf("%d", p[i][j]);
For the second example then p would need to be a pointer to an array, or a pointer to pointer.
It could be something like
int **p = malloc(m * sizeof(int *));
for (size_t i = 0; i < m; ++i)
{
p[i] = malloc(n * sizeof(int));
for (size_t j = 0; j < n; ++j)
{
scanf("%d", &p[i][j]);
}
}