c code to print reverse of array using recurion - c

This code is to print reverse of array using recursion
I am using recursion function called from main
output should be as 5,4,3,2,1
can someone help in debugging this
#include <stdio.h>
void recursion(int a[])
{
int i=0;
if(i<5)
return;
i++;
recursion(i);
printf("%d ",a[i]);
}
int main()
{
int arr[]={1,2,4,5};
recursion(arr);
}

A good C book is needed:
#include <stdio.h>
void recursion(int a[], int i, int size)
{
if(i < size -1)
recursion(a, i + 1, size);
printf("i = %d arr[%d] == %d \n",i, i, a[i]);
}
int main()
{
int arr[] = {1, 2, 4, 5};
recursion(arr, 0, sizeof(arr) / sizeof(arr[0]));
}

Your recursion function is not working properly.
Note whenever you are calling a function the parameters should be same. In your code parameter of your function is array, but when you are calling it from inside the function i.e recursion you are passing i.
So, what you can do is that
#include <stdio.h>
void recursion(int a[], int i)
{
int t = i-1;
if(t>=0)
{
printf("%d ",a[t]);
return recursion(a, t);
}
}
int main()
{
int arr[]={1,2,4,5};
recursion(arr,4);
}
Hopefully that helps

Related

min heap program stopped working

I am doing a simple minheap program but when size of heap is 4 i get an error program stopped working.
i checked heap size 2,3,5,6,7 program is working fine.
Why am i getting this error only when heapsize is 4?
I am using codeblocks 16.01, windows 10, gcc compiler.
minHeap.c
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
void MinHeapfy(int A[], int i, int N)
{
int l,r,temp,smallest;
l = 2*i;
r = 2*i + 1;
if((l<=N) && (A[l]<A[i]))
{
smallest = l;
}
else
{
smallest = i;
}
if((r<=N) && (A[r]<A[smallest]))
{
smallest = r;
}
if(smallest != i)
{
temp = A[smallest];
A[smallest] = A[i];
A[i] = temp;
MinHeapfy(A,smallest,N);
}
}
void BuildHeap(int A[],int N)
{
int i;
int f = floor(N/2);
printf("f %d", f);
for(i=f;i>=1;i--)
{
printf("i %d\n",i);
MinHeapfy(A,i,N);
}
}
int main()
{
int *A,N,T,data,q,i=1;
scanf("%d",&N);
A = (int *)calloc(N,sizeof(int));
for(i=1;i<=N;i++)
{
scanf("%d",&data);
A[i]=data;
}
BuildHeap(A,N);
for(i=1;i<=N;i++)
{
printf("%d ",A[i]);
}
return 0;
}
I can share some things that I learned till now:
void BuildHeap(int *A, int N);
void MinHeapfy(int *A, int i, int N);
In this 2 functions you just send a pointer not an array(difference between int *A and int A[5])
int main()
{
/*here it is better for you to declare like this
to see more easier which is which pointer and variable
(PS: at the moment you don't use q)*/
int N,T,data,i;//,q
int *A;
scanf("%d",&N);
A = (int *)calloc(N,sizeof(int));
for(i=0;i<N;i++)
{
scanf("%d",&data);
A[i]=data;
}
BuildHeap(A,N);
for(i=0;i<N;i++)
{
printf("%d ",A[i]);
}
return 0;
}
The 2 fors that you use must start from 0 to N
When you write:
A[0] <=> address pointed by A + 0 * sizeof(data type of A)
So 0 is the first address of the array
and the last address is N-1
A[N-1] <=> address pointed by A + (N-1) * sizeof(data type of A)

Using C to implement radix sort

I got stuck in the below question for a few days. I used C to implement radix sort, everything was fine except for one line of code. Could you please help me to solve this issue?
My problem is in the first line of radix_sort function. While I use int semi_sort[12], I can run the program correctly. However, I wanna use the size variable that I passed into the function, but when I use int semi_sort[size] instead of int semi_sort[12], my program would crash. Could anyone tell me why is that? BY the way, I referred to this link, in this author's codes, he did int semiSorted[size]. Why does this line of code work this time?
Thank you in advance!!
#include <stdio.h>
#include <stdlib.h>
#define bucket_size 10
int find_the_largest(int arr[],int size);
void display(int arr[],int size);
void radix_sort(int arr[],int size);
int main()
{
printf("------------------------------------------------------\n");
printf(" Hey! This is a radix sort algorithm!\n");
printf("------------------------------------------------------\n\n");
int array[] = {10, 2, 303, 4021, 293, 1, 0, 429, 480, 92, 2999, 14};
int size = sizeof(array)/sizeof(int);
int largest_num = find_the_largest(array,size);
printf("The unsorted array:");
display(array,size);
printf("The radix sort algorithm:\n\n");
radix_sort(array,size);
display(array,size);
return 0;
}
int find_the_largest(int arr[],int size){
int i,max_num=0;
for(i=0;i<size;i++){
if(arr[i]>max_num)
max_num = arr[i];
}
return max_num;
}
void display(int arr[],int size){
int i;
for(i=0;i<size;i++){
printf(" %d",arr[i]);
if(i==size-1)
printf("\n\n");
}
}
void radix_sort(int arr[],int size){
int semi_sort[12];
int max_num = find_the_largest(arr,size);
int i,significant_num = 1;
while(max_num/significant_num>0){
int bucket[bucket_size] = {0};
for(i=0;i<size;i++){
bucket[(arr[i]/significant_num)%10]++;
}
for(i=1;i<size;i++){
bucket[i] += bucket[i-1];
}
for(i=size-1;i>=0;i--){
semi_sort[--bucket[(arr[i]/significant_num)%10]] = arr[i];
}
for(i=0;i<size;i++)
arr[i] = semi_sort[i];
significant_num *= 10;
}
}
You have problem with code:
for(i=1;i<size;i++){
bucket[i] += bucket[i-1];
}
Because size can be greater then bucket_size.
And probably you have problem with:
for(i=size-1;i>=0;i--){
semi_sort[--bucket[(arr[i]/significant_num)%10]] = arr[i];
}
Because --bucket[(arr[i]/significant_num)%10] can be greater then 11 or less then 0.
And find_the_largest does not work correct on negative numbers.
You can dynamically allocate memory for your buffers, like this:
semi_sort = malloc(size * (sizeof *semi_sort));
And don't forget to free memory on end (free(semi_sort)).

Function printArray not working properly

I'm trying to make function that prints array but the output of it is wrong.
Can someone help me please?
This is the code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void printArr(int arr[],int size);
int main()
{
int arr1[3] = { 1,2,3 };
int arr2[5] = { 1,2,3,4,5 };
printf("arr1: \n");
printArr(arr1, 3);
printf("\n\narr2: \n");
printArr(arr2, 5);
printf("\n\n");
return(0);
}
void printArr(int arr[], int size)
{
int i;
for (i = 0; i < size; i++);
{
printf("%d", arr[i]);
}
}
What I get is:
Remove the semi-colon at the of for:
for (i = 0; i < size; i++);
^^^
That makes the for loop run size times and executes the block after that. But by that time, i value is equal to size. This leads to out of bound access, which is undefined behaviour. Clearly, this is not what you intention was.

Void function (C language)

I'm trying to run a very simple code using a void function, but no matter what I try or some error occurs, or the program doesn't print what it was supposed to. The code is
#include <stdio.h>
int main()
{
int i,j;
i = 1;
j = 2;
add(i, j);
return 0;
}
void add(int i, int j)
{
printf("%d + %d = %d", i, j, (i+j));
}
I am trying to use void in other more complex program so I am using this very simple to discover how to make it.
You need to give a prototype (or definition) of a function before you use it in a program.
Definition
Shift the function add before main function:
#include <stdio.h>
void add(int i, int j)
{
printf("%d + %d = %d", i, j , (i+j));
}
int main()
{
int i,j;
i = 1;
j=2;
add( i, j);
return 0;
}
Prototype
#include <stdio.h>
void add(int,int);
int main()
{
int i,j;
i = 1;
j = 2;
add(i, j);
return 0;
}
void add(int i, int j)
{
printf("%d + %d = %d", i, j, (i+j));
}
Change the order so that add is read first
#include <stdio.h>
void add(int i, int j)
{
printf("%d + %d = %d", i, j, (i+j));
}
int main()
{
int i,j;
i = 1;
j = 2;
add(i, j);
return 0;
}

Passing array to function using pointer

I'm trying to print array of pointer using pointer instead of array but I got this error Segmentation fault at runtime:
enter number of element:5
array[0]=1
array[1]=2
array[2]=3
array[3]=4
array[4]=5
Segmentation fault
This is the code:
#include <stdio.h>
#include <stdlib.h>
int *array;
int n;
void input(int *array,int n);
void display(int *array,int n);
int sum(int *array,int n);
int main (void) {
int result;
printf("enter number of element:");scanf("%d",&n);
input(array,n);
display(array,n);
result=sum(array,n);
printf("sum of array=%d",result);
return 0;
}
void input(int *array,int n){
int j;
array=(int *)malloc(n*sizeof(int));
for(j=0;j<n;j++){
printf("array[%d]=",j);scanf("%d",array+j);
}
}
void display(int *array,int n){
int j;
for(j=0;j<n;j++)
printf("%d\t",*(array+j));
printf("\n");
}
int sum(int *array,int n){
int sum=0,j;
for(j=0;j<n;j++)
sum+=*array+j;
return sum;
}
How can I fixed this code? please somebody explain me what's wrong with that code.
Variable array is a local variable in function input.
As such, it is pointless to set it with array = ..., because this assignment takes effect only inside the function. You should typically pass its address (&array) to any function that needs to change it.
In your specific example, you also have a global variable array, so a quick solution to your problem would be to simply call function input without passing variable array as an argument:
void input(int n)
{
...
array = (int*)malloc(n*sizeof(int));
...
}
int main()
{
...
input(n);
...
}
Note that this is a "dirty" workaround, and you should typically strive to avoid the use of global variables.
To add the clean version to barak's answer:
int input(int ** array, const size_t n)
{
int result = 0;
assert(NULL != array);
(*array) = malloc(n * sizeof(**array));
if (NULL == (*array))
{
result = -1;
}
else
{
size_t j;
for(j = 0; j < n; ++j)
{
printf("array[%zu]=", j);
scanf("%d", (*array) + j); /* still missing error checking here . */
}
}
return result;
}
And call it like this:
if (-1 == input(&array, n))
{
perror("input() failed");
exit(EXIT_FAILURE);
}
Try this input():
void input(int **array,int n){
int j;
*array=(int *)malloc(n*sizeof(int));
for(j=0;j<n;j++){
printf("array[%d]=",j);scanf("%d",*array+j);
}
}
Because C use pass-by-value, if you want to change the value of a variable in a function, you need to pass the address of that variable as the argument to that function.
In this case, you want to change the value of array in input() and the type of array is int *, therefore the prototype of input() should be something like void input (int **array, ...).
this should do..make sure you understand what the others have said..
#include <stdio.h>
#include <stdlib.h>
int *array;
int n;
void input(int **array,int n);
void display(int **array,int n);
int sum(int **array,int n);
int main (void) {
int result;
printf("enter number of element:");scanf("%d",&n);
input(&array,n);
display(&array,n);
result = sum(&array,n);
printf("sum of array= %d",result);
return 0;
}
void input(int **array,int n){
int j;
*array= malloc(n*sizeof(int));
for(j=0;j<n;j++){
printf("array[%d]=",j);
scanf("%d",(*array)+j);
}
}
void display(int **array,int n){
int j;
for(j=0;j<n;j++){
printf("%d\t",*((*array)+j)); // you can use array notation aswell
//array[0][j] will work
}
printf("\n");
}
int sum(int **array,int n){
int sum=0,j;
for(j=0;j<n;j++){
sum += *((*array)+j);
}
return sum;
}
What does *array + j do? Does it evaluate *array and add j to it? Or does it add j to array and then dereference it? Would you be willing to bet $100 on it if I told you you are wrong?
Make your life and the life of anybody reading your code easier by using parentheses, or even better, write array [j].

Resources