i wrote a program for counting sort using pointers
#include<stdio.h>
#include<stdlib.h>
int max(int *arr,int n)
{
int i;
int maxi=*arr;
for(i=0;i<n;i++)
{
if(*(arr+i)>maxi)
maxi=*(arr+i);
}
return maxi;
}
void count(int *arr,int n)
{
int i;
int s=max(arr,n);
printf("max %d",s);
int c[s];
memset(c,0,s*sizeof(int));
int b[n];
for(i=0;i<n;++i)
{
++c[arr[i]];
}
for(i=1;i<s;++i)
{
c[i]=c[i]+c[i-1];
}
for(i=n-1;i>=0;--i)
{
b[c[arr[i]]]=arr[i];
--c[arr[i]];
}
for(i=0;i<n;++i)
{
arr[i]=b[i];
}
}
void print(int *arr,int n)
{
int i;
for(i=0;i<n;i++)
printf("\nelement is %d ",*(arr+i));
}
int main()
{
int n,i;
int *arr=malloc(n*sizeof(int));
printf("\nenter the number of elements ");
scanf("%d ",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
count(arr,n);
print(arr,n);
return 0;
}
as i debug the program it stops at ++c[arr[i]] giving a segmentation fault. My call watches window looks like this
my inputs were like this
enter the number of elements 4
1
3
5
7
max 7
the code received segmentation fault after this also, the value of i is initialized to 0,then why is the watches window showing it's value as 4. is it fine to pass pointer subscripts to a static array or should i declare c dynamically?
Related
For some Reason the Program Crashes at the Loop used to merge the two arrays in main.
though maybe this question is a foolish in one(idk it didn't allow me to post as my program is mostly code so now i am blabbering).
(Well it still doesn't so here's a Picture of a Cat
CAT )
//To make two arrays of user Defined Size and merge them in a third array
#include<stdio.h>
#include<stdlib.h>
void array(int**,int);
void ini(int**,int);
void display(int **,int);
int main()
{
int *a1,*a2,*a3,s1,s2,i,j;
printf("Enter Size of Array 1\n");
scanf("%d",&s1);
printf("Enter Size of Array 2\n");
scanf("%d",&s2);
ini(&a1,s1);
ini(&a2,s2);
ini(&a3,s1+s2);
printf("Enter Elements of Array 1\n");
array(&a1,s1);
printf("Array 1:\n");
display(&a1,s1);
printf("Enter Elements of Array 2\n");
array(&a2,s2);
printf("Array 2:\n");
display(&a2,s2);
for(i=0;i<s1;i++)
{
a3[i]=a1[i];
}
for(i=0,j=s1;i<s2&&j<s1+s2;i++,j++)
{
a3[j]=a2[i];
}
printf("Merged Array:\n");
display(&a3,s1+s2);
return 0;
}
void ini(int **a,int s)
{
*a=(int*)calloc(s,sizeof(int));
}
void array(int **a,int s)
{
int i;
for(i=0;i<s;i++)
{
printf("Enter Element at position %d\n",i+1);
scanf("%d",&a[i]);
}
}
void display(int **a,int s)
{
int i;
for(i=0;i<s;i++)
{
if(i==s-1)
printf("%d\n",a[i]);
else
printf("%d\t",a[i]);
}
}
a1, a2, and a3 is a pointer of an integer type variable, which stores the starting address of your buffer.
a1[index] is the value in the address of a1 with 'index' as the offset.
Since you were passing &a to your array() and display() function, you were passing the address of your variable into the function instead of passing the address of your buffer.
You could change your array() and display() to:
void array(int *a,int s)
{
int i;
for(i=0;i<s;i++)
{
printf("Enter Element at position %d\n",i+1);
scanf("%d", &(a[i]));
}
}
void display(int *a,int s)
{
int i;
for(i=0;i<s;i++)
{
if(i==s-1)
printf("%d\n", a[i]);
else
printf("%d\t", a[i]);
}
}
and call it like this:
array(a1,s1);
display(a1,s1);
I have written a program in C for bucket sort:
#include <stdio.h>
#include <stdlib.h>
#define M 100
void insertion_sort(int arr[], int n){
int i, j, k;
for(i=1;i<n;i++){
j=i-1;
k=arr[i];
while(j>=0 && arr[j]>k){
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=k;
}
}
int max(int arr[], int n){
int max=arr[0];
for(int i=0;i<n;i++){
if(arr[i]>max)
max=arr[i];
}
return max;
}
void bucketsort(int arr[], int n){
int i, j, idx=0, *ptr, **bsort;
int len=sizeof(int *)*n+sizeof(int)*n*n;
bsort=(int **)malloc(len);
int len1=n*max(arr, n);
ptr=(int *)malloc(len1*sizeof(int));
for(i=0;i<n;i++){
*(ptr+i)=0;
}
for(i=0;i<n;i++){
int bindex=n*arr[i];
bsort[bindex][*(ptr+bindex)]=arr[i];
*(ptr+bindex)+=1;
}
for(i=0;i<n;i++){
if(*(ptr+i)>0)
insertion_sort(bsort[i], *(ptr+i));
}
for(i=0;i<n;i++){
for(j=0;j<*(ptr+i);j++)
arr[idx++]=bsort[i][j];
}
printf("The sorted array:\n");
for(i=0;i<n;i++){
printf("%d ", arr[i]);
}
}
int main()
{
int arr[M],n,i;
char op[1];
do{
printf("Enter no of elements: ");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
bucketsort(arr,n);
printf("\nDo you want to go again?(Y/N):");
scanf("%s", op);
}while(op[0]=='Y');
}
However, when I run the code, and give the necessary input, it gives me:
Segmentation fault (core dumped)
Why is this so? What sort of changes do I have to make in my code?
I've tried debugging on GDB, and it shows an error at the line
bsort[bindex][*(ptr+bindex)]=arr[i];
I don't understand why it should be so, since I have already written a max function for the same.
im trying to pass a 2D array from main to a function and trying to print it letter by letter
but it keeps giving me segmentation fault
note: the question im trying to solve as mentioned a function with parameter { ArrPrintMatrix(char *(p)[7]) }
so help me by keeping the above thing in mind
#include<stdio.h>
ArrPrintMatrix(char **p,int n) {
int i,j;
for(i=0;i<n;i++) {
for(j=0;j<10;j++) {
printf("%c ",p[i][j]);
}
}
}
main() {
int i;
char c[2][10];
puts("enter two strings");
for(i=0;i<2;i++)
scanf("%s",c[i]);
ArrPrintMatrix((char **) c,2);
}
You should use char p[2][10] not char** p
The following code could work:
#include <stdio.h>
void ArrPrintMatrix(char p[2][10], int n) {
int i;
for (i = 0; i < n; ++i)
printf("%s\n",p[i]);
}
int main() {
int i;
char c[2][10];
puts("enter two strings");
for(i=0;i<2;i++)
scanf("%s",c[i]);
ArrPrintMatrix(c,2);
return 0;
}
you need to change the type of the p var in the print function, and you should also set the array to zero so if the strings that are printing are less than 10 chars with terminator- garbage values are not displayed.
void ArrPrintMatrix(char p[][10],int n) {
int i,j;
for(i=0;i<n;i++) {
for(j=0;j<10;j++) {
printf("%c ",p[i][j]);
}
}
}
int main() {
int i;
char c[2][10]= {0};
puts("enter two strings");
for(i=0;i<2;i++)
scanf("%s",c[i]);
ArrPrintMatrix( c,2);
return 0;
}
I tried my hand in writing a program in C to "bubble sort" a sequence of numbers, obtained as input, using pointers. It was as follows:
#include<stdio.h>
void swap(int *p,int *q)
{
int t;
t=*p;
*p=*q;
*q=t;
}
void sort(int *a[],int n)
{
int i,j;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
swap(a[j],a[j+1]);
}
}
}
int main()
{
int p[40],b,i;
printf("Enter the number of elements in the sequence: \n");
scanf("%d",&b);
printf("Enter the elements of the sequence: \n");
for(i=0;i<b;i++)
{
scanf("%d",p[i]);
}
sort(p,b);
printf("The sorted sequence is: \n");
for(i=0;i<b;i++)
{
printf("%d \n",p[i]);
}
return 0;
}
However, the program did not compile. It showed the following error message:
The error message shows:
error 139 - Argument no 1 of 'sort' must be of type '<ptr><ptr>int', not 'int[40]'
Can anybody tell me how I should correct my program so that it gets compiled and gives a correct output?
ADDENDUM: The following is the corrected code, as asked for-
#include<stdio.h>
void myswap(int *p,int *q)
{
int t;
t=*p;
*p=*q;
*q=t;
}
void sort(int a[],int n)
{
int i,j;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
myswap(&a[j],&a[j+1]);
}
}
}
int main()
{
int p[40],b,i;
printf("Enter the number of elements in the sequence: \n");
scanf("%d",&b);
printf("Enter the elements of the sequence: \n");
for(i=0;i<b;i++)
{
scanf("%d",&p[i]);
}
sort(p,b);
printf("The sorted sequence is: \n");
for(i=0;i<b;i++)
{
printf("%d \n",p[i]);
}
return 0;
}
From a short look two mistakes found:
void sort(int *a[],int n)
should be
void sort(int a[],int n)
and
swap(a[j],a[j+1])
should be
swap(&a[j],&a[j+1])
a[j] is just an integer you need to take the address of the element placing & since swap declaration requires pointers.
I'm trying to create a simple(?) selection sort program in C that selects the largest integer of an integer array and places it in the location a[n-1], places the second largest number in a[n-2], etc until the smallest number is placed in a[0]. I've run through the below code on paper and it seems like it should work, but when I compile it I'm getting faulty results. Am I missing something obvious?
/* The program implements selection sort*/
#include <stdio.h>
#include "simpio.h"
#define n 5
void GetArray(int a[]);
void SelectionSort(int a[]);
int FindMax(int a[], int high);
void swap(int a[], int p1, int p2);
void PrintArray(int a[]);
main()
{
int a[n];
GetArray(a);
SelectionSort(a);
PrintArray(a);
getchar();
}
void GetArray(int a[])
{
int i;
for(i=0;i<n;i++)
{
printf("Enter integer# %d", i+1);
a[i]=GetInteger();
}
}
void SelectionSort(int a[])
{
int i, max;
for(i=0;i<n;i++)
{
max=FindMax(a,i);
swap(a,max,(n-1-i));
}
}
int FindMax(int a[], int high)
{
int i, index;
index=high;
for(i=high;i<n;i++)
{
if(a[i]>a[index])
index=i;
}
return index;
}
void swap(int a[], int p1, int p2)
{
int temp;
temp=a[p2];
a[p2]=a[p1];
a[p1]=temp;
}
void PrintArray(int a[])
{
int i;
for(i=0;i<n;i++)
printf("a[%d]=%d\n", i, a[i]);
}
Change these method to:
void SelectionSort(int a[])
{
int i, max;
for(i=0;i<n;i++)
{
max=FindMax(a,n-i-1);
swap(a,max,n-i-1);
}
}
int FindMax(int a[], int high)
{
int i, index;
index=high;
for(i=0;i<high;i++)
{
if(a[i]>a[index])
index=i;
}
return index;
}
I actually tested my answer and it works.
Selection sort is process of comparing minimum element from the list and placing from the least index.
Now consider below code snippet.
public void selectionSort(int[] elements) {
for(int i=0;i<elements.length;i++) {
int minPosition = i;
for(int j=i+1;j<elements.length;j++) {
if(elements[minPosition]>elements[j])
minPosition = j;
}
int temp = elements[i];
elements[i] = elements[minPosition];
elements[minPosition] = temp;
}
}
Thanks for reading, let me know feedback to improve from myside
Shouldn't:
max=FindMax(a,i);
swap(a,max,(n-1-i));
Be:
max=FindMax(a,i);
swap(a,max,i);
otherwise, next time through the loop, you'll find the same max value in the top position in the array.
A very basic implementation of selection sort
#include<stdio.h>
main()
{
int i,j,n=7,a[]={1,2,5,3,8,9,5},key;
for(j=1;j<n;j++)
{
key=a[j]; //a[j] is the new element to be added to the sorted
//sequence
i=j-1;
while(i>=0 && key<a[i]) //traverse through the sorted sequence
{a[i+1]=a[i];i--;} //until the place of key is found
a[i+1]=key;
}
for (j=0;j<n;j++)
printf("%d",a[j]);
}
#include<stdio.h>
#include<conio.h>
int removex(int arr[],int small,int n)
{
int i=0;
for(;i<n;i++)
if(arr[i]==small) //searching that no to delete
break;
for(;i<n-1;i++)
arr[i]=arr[i+1]; //delete by overloading no
return n-1;
}
void selectSort(int arr[],int sort[],int n)
{
int j=0,k=0,small;
while(n!=0)
{
small=arr[0];
for(j=0;j<n;j++)
if(arr[j]<small)
small=arr[j]; //finding smallest no
sort[k++]=small;
n=removex(arr,small,n); //removing that from list as we included that no into sorted list
}
}
void main()
{
int arr[10],arr2[10],i,n;
clrscr();
printf("Enter how many elements");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
selectSort(arr,arr2,n);
printf("sorted list is\n");
for(i=0;i<n;i++)
printf("%d\n",arr2[i]);
getch();
}