So I'm trying to print the truth table of a gate with n inputs using linked lists. I tried the following but I'm running into a problem. If I use the for loops with bincombs, to print all possible combinations and to calculate the result, on both report and myandlst as shown below, it crashes. The problem is easily fixed if I print everything in myandlst but I want to avoid it. Here is my code so far:
#include <stdio.h>
#include <stdlib.h>
typedef struct data
{
int value;
struct data * next;
} Data;
typedef Data * DataList;
typedef int (*CallBack)(DataList *inlist, int n);
int report(CallBack f, int n);
int getbit(int x, int p);
void bincombs(int * x, int n);
int myandlst(DataList *list, int n);
int main( )
{
CallBack f ;
report(myandlst, 4);
return 0;
}
int getbit(int x, int p)
{
return (x & (1<<p))!=0;
}
void bincombs(int * x, int n)
{
static int state = 0 ;
int i;
for (i=0; i<n; i++)
{
*x = getbit (state, i);
x++;
}
state ++;
return;
}
Data * createData( int value)
{
Data * dataptr;
dataptr = malloc(sizeof (Data));
dataptr->value = value;
dataptr->next = NULL;
return dataptr;
}
void appendData(DataList *lstptr, Data *newptr)
{
if (*lstptr==NULL)
{
*lstptr = newptr;
return;
}
appendData( &((*lstptr)->next), newptr);
return;
}
int myandlst (DataList *inlist, int n)
{
int i,j,k;
int * x = malloc (n*sizeof(int));
k=1;
for (i=0; i< (1<<n) ; i++)
{
bincombs(x, n);
for (j=n-1; j>=0; j--)
{
k*=x[j];
}
appendData(inlist,createData(k));
k=1;
}
return 0;
}
int report(CallBack f, int n)
{
DataList temp ;
int * x = malloc (n*sizeof(int));
int i,j;
f(&temp, n);
for (i=0; i< (1<<n) ; i++)
{
bincombs(x, n);
for (j=n-1; j>=0; j--)
printf("%d ", x[j]);
printf("%d\n", temp -> value);
temp = temp->next;
}
printf("\n");
return 0;
}
P.S This is an assignment and therefore the structure of the program is standard. I can't avoid using linked lists or changing the format of the functions too much.
Related
With an input of an integer 4, for example, I'm trying to get the following output w/ the initializePoly function.
(0, 0)
(-1, 1)
(-2, 4)
(-3, 9)
Instead, I'm getting all 0s in the outputs' second value when I run the code. Any feedback much appreciated.
#include <stdio.h>
#include <stdlib.h>
struct point{
int x;
int y;
};
void printPoint(struct point);
void printPoly(struct point *, int);
void initializePoly(struct point *, int);
int main(void) {
// Fill in your main function here
struct point * polygon;
int num;
scanf("%d", &num);
polygon = (struct point *) malloc(num * sizeof(struct point));
initializePoly(polygon, num);
printPoly(polygon, num);
free(polygon);
}
void printPoint(struct point pt) {
printf("(%d, %d)\n", pt.x, pt.y);
}
void printPoly(struct point *ptr, int N) {
int i;
for (i=0; i<N; i++) {
printPoint(ptr[i]);
}
}
// Write your initializePoly() function here
void initializePoly(struct point *pt, int num){
int i;
for(i=0;i<num;i++)
pt[i].x = -i;
pt[i].y = i*i;
}
void initializePoly(struct point *pt, int num){
int i;
for(i=0;i<num;i++)
pt[i].x = -i;
pt[i].y = i*i;
}
same as
void initializePoly(struct point *pt, int num){
int i;
for(i=0;i<num;i++) {
pt[i].x = -i;
}
pt[i].y = i*i;
}
Use {} to iterate both assignments.
void initializePoly(struct point *pt, int num){
int i;
for(i=0;i<num;i++) {
pt[i].x = -i;
pt[i].y = i*i;
}
}
I tried to build a heap and finally print the elements in the form of an array.
Here it is the code (I know this doesn't really make sense but I just wanted to test my knowlwdge of heap and dynamic arrays):
#include <stdio.h>
#include <stdlib.h>
void heapiify(int *arr,int n, int i)
{
int largest=i;
int l=2*i+1; // left node
int r= 2*i+2; // right node
if(l<=n && *arr[l]>=*arr[i])
largest=l;
if (r <=n && *arr[r]<=*arr[i])
largest= r;
if(largest !=i)
{
int temp=*arr[i];
*arr[i]=*arr[largest];
*arr[largest]=temp;
}
heapify(*arr,n,largest);
}
void buildh(int *arr,int n,int r,int c)
{
int i;
for(i=n/2-1;i>=0;i--)
heapify(*arr,n,i);
output(*arr,r,c);
}
void output(int *arr,int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d",*arr[i*c+j]);
}
printf("\n");
}
}
int main()
{
int i,j,r,c;
printf("enter the number of rows");
scanf("%d",&r);
printf("enter the number of columns");
scanf("%d",&c);
int n=r*c;
int *arr=malloc(n*sizeof(int));
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
scanf("%d",&arr[i*c+j]);
}
buildh(*arr,n,r,c);
}
I'm getting 9 errors which are all the same
invalid argument type of unary '*'( have int)
Your arr variable is of type pointer to int:
int *arr=malloc(n*sizeof(int));
So when you call buildh, which takes the same type, you have to pass it as-is:
buildh(arr,n,r,c);
Same for the other cases.
The problem is the dereference of arr, across your funtions in multiple places, and the passing of dereferenced *arr in your functions to int * parameters, you should pass arr, try:
//...
void heapify(int *arr, int n, int i)
{
int largest = i;
int l = 2 * i + 1; // left node
int r = 2 * i + 2; // right node
if (l <= n && arr[l] >= arr[i]) //here
largest = l;
if (r <= n && arr[r] <= arr[i]) //here
largest = r;
if (largest != i)
{
int temp = arr[i]; //here
arr[i] = arr[largest]; //here
arr[largest] = temp; //here
}
heapify(arr, n, largest); //here
}
void buildh(int *arr, int n, int r, int c)
{
int i;
for (i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i); //here
output(arr, r, c); //here
}
void output(int *arr, int r, int c)
{
int i, j;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%d", arr[i * c + j]); //here
}
printf("\n");
}
}
int main()
{
//...
buildh(arr, n, r, c); //here
}
I tried to reverse an array using only pointers. The program runs without any errors but it doesn't reverse the array. What's wrong with my code?
#include <stdio.h>
#define SIZE 10
void arrayReverseOutput(int * arr);
void arrayInput(int * arr);
void printArray(int * arr);
int main(void)
{
int arr[SIZE] = { 0 };
arrayInput(arr);
arrayReverseOutput(arr);
printArray(arr);
system("PAUSE");
return 0;
}
void arrayInput(int * arr){
int i = 0;
for (i = 0; i < SIZE; i++){
scanf("%d",(arr+i));
}
}
void arrayReverseOutput(int * arr){
int i = 0;
int k = SIZE-1;
int temp = 0;
for (i = 0; i < SIZE; i++){
temp = *(arr+i);
*(arr+i) = *(arr + k);
*(arr + k) = temp;
k--;
}
}
void printArray(int * arr){
int i = 0;
for (i = 0; i < SIZE; i++){
printf("%d ", *(arr+i));
}
}
The problem is, you are actually reversing the array twice, negating any changes achieved by swapping places.
Loop over only half of the array while swapping the elements, like
for (i = 0; i < SIZE/2; i++)
You can use 2 pointers to do so
void arrayReverseOutput(int *head, int *tail)
{
int temp = 0;
do
{
temp = *tail;
*tail = *head;
*head = temp;
}
while (head++ < tail--);
}
Complete code
#include <stdio.h>
#define SIZE 10
void arrayReverseOutput(int *head, int *tail);
void arrayInput(int * arr);
void printArray(int * arr);
int main(void)
{
int arr[SIZE] = { 0 };
arrayInput(arr);
arrayReverseOutput(arr, &arr[SIZE - 1]);
printArray(arr);
return 0;
}
void arrayInput(int * arr)
{
int i = 0;
for (i = 0; i < SIZE; i++)
{
scanf("%d", (arr + i));
}
}
void arrayReverseOutput(int *head, int *tail)
{
int temp = 0;
do
{
temp = *tail;
*tail = *head;
*head = temp;
}
while (head++ < tail--);
}
void printArray(int * arr)
{
int i = 0;
for (i = 0; i < SIZE; i++)
{
printf("%d ", *(arr + i));
}
}
I am trying to access a structure with a pointer to an integer , from main. But the program crashes. It needs to be built with "std=c99" option as it is the requirement in a test.
The code is as follows:
#include <stdio.h>
#include <malloc.h>
struct Results{
int *A;
int N;
};
struct Results solution(int A[], int N, int K) {
struct Results result;
// write your code in C99 (gcc 4.8.2)
int* T = (int*) malloc(N*sizeof(int));
result.A = A;
result.N = N;
int count = 0;
while(count < K)
{
for(int i = 0; i < N; i++)
{
if(i > 0)
{
T[i] = A[i-1];
}
else
{
T[0] = A[N-1];
}
}
count++;
for(int i = 0; i < N; i++)
{
A[i] = T[i];
}
};
for(int i = 0;i < N; i++)
{
A[i] = T[i];
}
return result;
}
struct Results solution(int A[], int N, int K);
void main()
{
int B[5] = {3,8,9,7,6};
struct Results st;
solution(B,sizeof(B),1);
}
The trouble is at line:
" solution(B,sizeof(B),1);"
What am I doing wrong?
Please help.
You see sizeof(B) would give the number of elements in B times the size of an int, use sizeof(B) / sizeof(B[0]) instead.
i declare a global variable and use and modify its value in the function. Then i want to get the modified value of this global variable, it has some problem. Can anyone help me?
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 10
struct link
{
int freq;
char value[MAX];
struct link* right;
struct link* left;
};
typedef struct link node;
void sort(node *[], int);
node* create(char[], int);
void sright(node *[], int);
void Assign_Code(node*, int [], int);
void Delete_Tree(node *);
int test[720][720];
main()
{
node* ptr, * head;
int i, n, total = 0, u, c[256];
char str[MAX];
node* a[256];
int freq;
printf( "Huffman Algorithm\n");
printf("\nEnter the no. of letter to be coded:");
/*input the no. of letters*/
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("Enter the letter & frequency:");
/*input the letter & frequency*/
scanf("%s %d", str, &freq);
a[i] = create(str, freq);
}
while (n > 1)
{
sort(a, n);
u = a[0]->freq + a[1]->freq;
strcpy(str,a[0]->value);
strcat(str,a[1]->value);
ptr = create(str, u);
ptr->right = a[1];
ptr->left = a[0];
a[0] = ptr;
sright(a, n);
n--;
}
Assign_Code(a[0], c, 0);
//getch();
printf("Code: ");
for (i = 1; i <= n; i++)
{
printf("%d", test[0][i]);
}
printf("\n");
Delete_Tree(a[0]);
}
node* create(char a[], int x)
{
node* ptr;
ptr = (node *) malloc(256*sizeof(node));
ptr->freq = x;
strcpy( ptr->value , a);
ptr->right = ptr->left = NULL;
return(ptr);
}
void sort(node* a[], int n)
{
int i, j;
node* temp;
for (i = 0; i < n - 1; i++)
for (j = i; j < n; j++)
if (a[i]->freq > a[j]->freq)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
void sright(node* a[], int n)
{
int i;
for (i = 1; i < n - 1; i++)
a[i] = a[i + 1];
}
void Assign_Code(node* tree, int c[], int n)
{
int i;
if ((tree->left == NULL) && (tree->right == NULL))
{
printf("%s code: ", tree->value);
test[0][0]=tree->value;
for (i = 0; i < n; i++)
{
test[0][i+1]=c[i];
printf("%d", c[i]);
}
printf("\n");
}
else
{
c[n] = 1;
n++;
Assign_Code(tree->left, c, n);
c[n - 1] = 0;
Assign_Code(tree->right, c, n);
}
}
void Delete_Tree(node * root)
{
if(root!=NULL)
{
Delete_Tree(root->left);
Delete_Tree(root->right);
free(root);
}
}
Let me highlight the problem:
while (n > 1)
{
...
n--;
}
...
for (i = 1; i <= n; i++)
{
printf("%d", test[0][i]);
}
By the time the second loop starts n is one, and the printf is executed only once, so that you print the value of test[0][1] and only that.
The value of test[0][1] is overwritten many times (as many as the number of leaf nodes in the tree) in Assign_Code:
void Assign_Code(node* tree, int c[], int n)
{
if ((tree->left == NULL) && (tree->right == NULL))
{
...
for (i = 0; i < n; i++)
{
test[0][i+1]=c[i];
}
}
...
}
Now, because of the way you are traversing the tree the last time test[0][1] is overwritten is for a Huffman code that has a '0' as first character.
As a side note:
node* create(char a[], int x)
{
node* ptr;
ptr = (node *) malloc(256*sizeof(node)); // <--- This is wrong
ptr->freq = x;
strcpy( ptr->value , a);
ptr->right = ptr->left = NULL;
return(ptr);
}
There is no reason to allocate 256 times the size of node to store one node. You are creating a node and storing it in an array of pointers to nodes. Allocate one node there, like this:
malloc (sizeof (node)); or malloc ((sizeof (*ptr));
you can store the n value to some temporary variables, after you get the
values.
Then use the temporary variable in your for loop condition.
scanf("%d", &n);
int temp = n ;
for (i = 1; i <= temp ; i++)
{
printf("%d", test[0][i]);
}