i want to pass a function within function by reference in c.They both use the same parameters.This is the code
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void AtoB(int *A, int *B, int *C, int n,int *h1,int *h2,int *h3 );
void AtoC(int *A, int *B, int *C, int n,int *h1,int *h2,int *h3 );
void BtoC(int *A, int *B, int *C, int n,int *h1,int *h2,int *h3 );
void function2(int *A, int *B, int *C, int n);
int main(){
int n;
int e;
int h3=1;
int h2=1;
int h1=0;
int min;
int *A;
int *B;
int *C;
printf("Give me the number of disks:");
scanf("%d",&n);
A = (int *) calloc(n,sizeof(int));
B = (int *) calloc(n,sizeof(int));
C = (int *) calloc(n,sizeof(int));
min=pow(2,n)-1;
for (e=0;e<n;e++){
A[e]=e+1;
}
if (n%2==0){
for (e=0;e<min/3;e++){
AtoB(A,B,C,n,&h1,&h2,&h3);
}
}
free(A);free(B);free(C);
return 0;
}
int function1(int Z[],int n){
int j,i,k,a;
for (i=0;i<n;i++){
k=n-Z[i];
for (j=0;j<n;j++){
if(k==j){
for(a=0;a<2*Z[i]-1;a++){
printf("%d",Z[i]);
}
}
else if((j==n-1)&&(Z[i]==0)){
printf("|");
}
else{
printf(" ");
}
}
printf("\n");
}for(i=0;i<2*n-1;i++){
printf("-");
}
printf("\n\n");
return 0;
}
void function2(int A[],int B[],int C[],int n){
printf("A\n");
function1(A,n);
printf("B\n");
function1(B,n);
printf("C\n");
function1(C,n);
}
void AtoB(int A[],int B[],int C[],int n,int *h1,int *h2,int *h3){
if (B[n-1]==0){
printf("A->B\n");
B[n-1]=A[*h1];
A[*h1]=0;
*h1=*h1+1;
function2(A,B,C,n);}
else if (A[n-1]==0){
printf("A->B\n");
A[0]=B[n-*h2];
B[n-*h2]=0;
*h2=*h2-1;
function2(A,B,C,n);
}
AtoC(A,B,C,n,&h1,&h2,&h3);
}
void AtoC(int A[], int B[], int C[], int n,int *h1,int *h2,int *h3 ){
}
The program must solve hanoi tower while showing the tower structure.
I am getting error: passing argument 5 of 'AtoC' from incompatible pointer type.Thanks in advance.
You can't do like that, In AtoB function you have declared h1 as a pointer and in AtoC function you are passing address of that pointer. Both functions should have same declaration like that *h1,*h2,*h3.
Related
I don't have idea where is the problem but the latest pointer(vector) have some troubles.
First value it's ok (V[0]+T[0]) , S[1] it's always 0 and third value it's random.
#include <stdio.h>
#include <stdlib.h>
int citire_vector(int n, int *V);
void afisare_vector(int n, int *V);
int produs_scalar(int n, int *V, int *T);
int suma_vectori(int n, int *V, int *T);
int main(void)
{
int n, *X, *Y, ps, *S;
printf("n = ");
scanf("%d",&n);
X = (int*) malloc(n*sizeof(int));
Y = (int*) malloc(n*sizeof(int));
citire_vector(n,X);
citire_vector(n,Y);
afisare_vector(n,X);
afisare_vector(n,Y);
ps = produs_scalar(n,X,Y);
printf("Produsul scalar = %d\n",ps);
S = (int*) malloc(n*sizeof(int));
*S= suma_vectori(n,X,Y);
afisare_vector(n,S);
}
int citire_vector(int n, int *V)
{
int i;
for(i=0;i<n;i++)
scanf("%d",V+i);
return *V;
}
void afisare_vector(int n, int *V)
{
int i;
printf("Valorile vectorului sunt:\n");
for(i=0;i<n;i++)
printf("%d ",*(V+i));
printf("\n");
}
int produs_scalar(int n, int *V, int *T)
{
int i, ps = 0;
for(i = 0;i<n;i++)
ps += (*(V+i))*(*(T+i));
return ps;
}
int suma_vectori(int n, int *V, int *T)
{
int i, *U;
for(i=0;i<n;i++)
{
*(U+i )= *(V+i);
}
return *U;
}
Your suma_vectori and its usage are incorrect.
Pointer U inside suma_vectori is uninitialized, causing undefined behavior on assignment
Assignment *S= suma_vectori(n,X,Y) has no effect beyond the initial element of S
To fix this problem, change suma_vectori to return int*, move malloc of the result inside the function, remove malloc for S, and assign S the result of the suma_vectori call:
int *suma_vectori(int n, int *V, int *T); // forward declaration
int *suma_vectori(int n, int *V, int *T) { // Implementation
int *U = malloc(n*sizeof(int)); // do not cast malloc
for(int i=0;i<n;i++) {
U[i] = V[i] + T[i];
}
return U;
}
// call
S= suma_vectori(n,X,Y);
// Don't forget to free malloc-ed memory
free(X);
free(Y);
free(S);
You have to allocate memory to U in suma_vectori function
as it is picking garbage value
The point of the program is to send data from 1 array to another array I'm not sure what's wrong with how I'm passing it. It should enter the data in 1 array then call upon the copy function and puts itself there and then the array is traversed.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<limits.h>
#include<math.h>
#include<ctype.h>
#include<stdbool.h>
double copy_arr(double source[n][u],double target[n][u],int n,int u);
int main(void)
{
double source[3][5]={{1.1,2.2,3.3,4.4,5.5},
{1.1,2.2,3.3,4.4,5.5},
{1.1,2.2,3.3,4.4,5.5}};
double target1[3][5];
copy_arr(source,target1,3,5);
int j;
int i;
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
{
printf("%f 1",target1[i][j]);
}
}
return 0;
}
double copy_arr(double source[][],double target[][],int n,int u)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<u;j++)
{
target[i][j] = source[i][j];
}
}
return target[n][u];
}
Your function prototype is wrong because compiler has not seen n and u yet. Your program does not even compile.
Change
double copy_arr(double source[n][u],double target[n][u],int n,int u);
to
double copy_arr(int n,int u,double source[n][u],double target[n][u]);
Or you could do hardcoded array size
#define SIZE_ARR 5
void copy_arr(double source[][SIZE_ARR], double target[][SIZE_ARR], int n, int u);
int main(void)
{
double source[3][SIZE_ARR]={{1.1,2.2,3.3,4.4,5.5},
{1.1,2.2,3.3,4.4,5.5},
{1.1,2.2,3.3,4.4,5.5}};
double target1[3][SIZE_ARR];
copy_arr(source,target1, 3, 5);
int j;
int i;
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
{
printf("%f 1",target1[i][j]);
}
}
return 0;
}
void copy_arr(double source[][SIZE_ARR], double target[][SIZE_ARR], int n, int u)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<u;j++)
{
target[i][j] = source[i][j];
}
}
}
I am trying to merge to arrays without sorting (add one then another) using pointer method but its just printing the first array and then garbage values. What i am trying to do is just combine 2 arrays in one big array. No sorting required(at least for now).
void getarray(int*,int);
int merge(int*,int*,int,int,int*);
main()
{
int a[10],b[10],c[20];
int i,j,n,m,size;
clrscr();
printf("Enter no. of elements in FIRST array: ");
scanf("%d",&n);
getarray(a,n);
printf("Enter no. of elements in SECOND array: ");
scanf("%d",&m);
getarray(b,m);
merge(a,b,n,m,c);
printf("\nMerged Array: \n");
for(i=0;i<n+m;i++)
{
printf("\t%d\t",c[i]);
}
getch();
return 0;
}
void getarray(int *x, int y)
{
int i;
for(i=0;i<y;i++)
{
scanf("%d",x+i);
}
}
int merge(int *a, int *b,int n,int m,int *c)
{
int i,j;
for(i=0;i<n;i++)
{
*(c+i) = *(a+i);
}
for(j=i;j<i+m;j++)
{
*(c+j) = *(b+j);
}
}
Alternatively you can use (assuming c is large enough):
void merge(int *a, int *b,int n,int m,int *c) {
memcpy(c, a, sizeof(int)*n);
memcpy(c+n, b, sizeof(int)*m);
}
You would need to include string.h.
int merge(int *a, int *b,int n,int m,int *c)
{
int i,j;
for(i=0;i<n;i++)
{
*(c+i) = *(a+i);
}
for(j=0;j<m;j++)
{
*(c+n+j) = *(b+j);
}
}
So I'm trying to write a program that finds the union, intersection, and determining if b is a subset of a for two groups of characters entered.
So if I enter
abcd for a[]
and
ac for b[]
It should print:
intersection: ac
union: abcd
"b is a subset of a".
My code is working for the most part, but my union function isn't giving an output. Do you have any suggestions for fixing it? I ran through it and it seems to be executing, but d isn't printing for someone... is my boolean statement wrong?
My code:
#include <stdio.h>
#include "simpio.h"
#include "genlib.h"
#include "strlib.h"
#define n 26
/* typedef enum letters {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z};
letters letter; */
bool intersection(bool a[], bool b[], bool c[]);
void GetSet(bool set[]);
void init(bool a[]);
void printArray(bool set[]);
void getunions(bool a[], bool b[], bool d[]);
void contain(bool a[], bool b[]);
main()
{
bool a[n], b[n], c[n], d[n];
init(a);
init(b);
init(c); /* intersection */
init(d); /* union */
printf("Entries for a = \n");
GetSet(a);
printf("Entries for b = \n");
GetSet(b);
intersection(a,b,c);
printArray(c);
getunions(a,b,d);
printArray(d);
contain(a,b);
getchar();
}
void init(bool set[])
{
int i;
for(i=0;i<n;i++)
{
set[i]=FALSE;
}
}
void GetSet(bool set[])
{
int i;
string str=GetLine();
int len=StringLength(str);
for(i=0;i<len;i++)
{
set[str[i]-97]=TRUE;
}
}
bool intersection(bool a[], bool b[], bool c[])
{
int i;
for(i=0;i<n;i++)
{
if(b[i]&&a[i]==TRUE) c[i]=TRUE;
}
printf("\n\nThe intersection is\n");
return c;
}
void getunions(bool a[], bool b[], bool d[])
{
int i;
for(i=0;i<n;i++)
{
if(a[i]==TRUE||b[i]==TRUE)
d[i]==TRUE;
}
}
void printArray(bool set[])
{
int i;
for(i=0;i<n;i++)
{
if(set[i])
printf("%c", i+97);
}
}
void contain(bool a[], bool b[])
{
int i;
bool flag;
for(i=0;i<n;i++)
{
if(a[i]&&b[i]) flag=TRUE;
}
if(flag) printf("\n\nb is a subset of a.\n");
else printf("\n\nb is not a subset of a.\n");
}
In getunions, d[i]==TRUE; should be d[i]=TRUE;. You want an assignment, not a comparison.
As an aside, I think the contain function is incorrect. A) it does not initialize flag, and B) it says "b is a subset of a" as long as a and b share at least one common element.
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();
}