Unexpected output of function in C - c

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;
}
}

Related

Issue with linked list and truth table

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.

Passing pointer to an array as a parameter to a function

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
}

Expression must have arithmetic type

While trying to write a code to find the intersection of two arrays,I came across a problem. I can't seem to modify a pointer inside a function.
Inside my find_intersection I get the error while doing the realloc function,compiler states that "counter" has no arithmetic value.
Any explanation on what went wrong here?
#include <stdio.h>
#include <stdlib.h>
int quick_sort(int*, int, int);
void swap(int*, int*);
int partition(int *, int, int);
int input_array_dyn(int*n);
int *find_intersection(int*, int*, int*, int, int,int *);
main()
{
int size1, size2, *counter, i=0;
int *arr1 = input_array_dyn(&size1);
int *arr2 = input_array_dyn(&size2);
quick_sort(arr1, 0, size1 - 1);
quick_sort(arr2, 0, size2 - 1);
int *arr3 = (int*)calloc(size2, sizeof(int));
arr3= find_intersection(arr1, arr2, arr3, size1, size2, &counter);
printf("The size of the new array is:%d\n", counter);
while (i < counter)
{
printf("%d\n", arr3[i]);
i++;
}
free(arr1);
free(arr2);
free(arr3);
}
int *find_intersection(int *arr1, int *arr2, int *arr3, int SA, int SB, int *counter)
{
int i = 0, j = 0, n = 0;
*counter = 0;
while (i < SA &&j < SB)
{
if (arr1[i] < arr2[j])
i++;
else if (arr2[j] < arr1[i])
j++;
else
{
arr3[n] = arr1[i];
i++;
n++;
j++;
}
}
counter = n;
arr3 = (int*)realloc(arr3, counter*sizeof(int));/*error here*/
return arr3;
}
int input_array_dyn(int*n)
{
int i;
int *a;
printf("Enter the size of the array:\n");
scanf("%d", n);
a = (int*)calloc(*n, sizeof(double));
assert(a);
printf("Enter the array elements:%d.\n", *n);
for (i = 0; i < *n; i++)
scanf("%d", a + i);
return a;
}
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition(int *arr, int low, int high)
{
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++)
{
if (arr[j] <= pivot)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
int quick_sort(int *arr, int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
quick_sort(arr, low, pi - 1);
quick_sort(arr, pi + 1, high);
}
}
In find_intersection() counter is a pointer to an int. To change it's value you need to use *counter instead of counter.
return arr3; attempts to return a pointer to int while function is declared to return just int. counter is a pointer to an int while you are using it as an regular int setting to 0 and so on.

[C Programming]Vectors & Pointers

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

pointer being realloc'd was not allocated

Actually this is a pure C prog.when i compile with Xcode. error
message says "pointer being realloc'd was not allocated"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int LocateElem(int *p1,int e,int leng1);
void Display(int max, int array[]);
int GetElem(int * p, int pass);
int Union(int *p1,int *p2, int leng1, int leng2);
int ListInsert(int *p, int e, int lengA);
int* GetData(int* pArray, int Array_size);
void Show(int *p, int leng);
void InitList_Sq(int *L);
int *p_A,*p_B;
int m,n;
int main()
{
clock_t begin, end;
double cost;
begin = clock();
printf("How many elements of A u want:");
scanf("%d",&m);
if (m<0) {
printf("Error!");
return 0;
}
printf("How many elements of B u want:");
scanf("%d",&n);
if (n<0) {
printf("Error!");
return 0;
}
p_A=(int *)malloc(m*sizeof(int));
p_B=(int *)malloc(n*sizeof(int));
if (p_A==NULL) {
printf("Error allocating memory!\n"); //print an error message
return 0; //return with failure
}
if (p_B==NULL) {
printf("Error allocating memory!\n"); //print an error message
return 0; //return with failure
}
int *pLast_A, * pLast_B;
printf("Array A is :\n");
pLast_A=GetData(p_A, m);
printf("\nArray B is :\n");
pLast_B=GetData(p_B, n);
int newLeng;
newLeng=Union(p_A,p_B,m,n);
printf("\nThe Union set is :\n");
Show(p_A, newLeng);
free(p_A);
free(p_B);
end = clock();
cost = (double)(end - begin) / CLOCKS_PER_SEC;
printf("\n%lf seconds", cost);
return 1;
}
int* GetData(int* pArray, int Array_size){
int* pFill= pArray;
int count;
srand((unsigned) time(NULL));
for ( count=0; count< Array_size; count++) {
*(pFill+count)=rand()%1000;
printf("%d\t", * (pFill+count));
}
return pFill+count;
}
int Union(int *p1,int *p2, int leng1, int leng2){
for (int count=0; count<leng2; count++) {
int e=GetElem(p2, count);
while(LocateElem(p1, e, leng1)==0){
leng1=ListInsert(p1, e, leng1);
}
}
return leng1;
}
int GetElem(int *p, int pass){
return *(p+pass);
}
int LocateElem(int *p1,int e,int leng1){
for (int count=0; count<leng1; count++)
if (e==*(p1+count))
return 1;
else
return 0;
}
int ListInsert(int *p, int e, int lengA){
lengA+=1;
int* temp;
temp=(int*)realloc(p, lengA*sizeof(int));
if (temp==NULL) {
printf("Error allocating memory!\n"); //print an error message
free(temp);
return 0; //return with failure
}
else{
p=temp;
*(p+lengA-1)=e;
}
return lengA;
}
void Show(int *p, int leng){
for (int count=0; count<leng; count++) {
printf("%d\t", *(p+leng));
}
}
After compilation xcode gives the breakpoint at the line temp=(int*)realloc(p, lengA*sizeof(int)) with signal SIGABRT.
The problem is that here:
int ListInsert(int *p, int e, int lengA){
int* temp;
temp=(int*)realloc(p, lengA*sizeof(int));
...
else {
p=temp; // <<<<< THIS
the new value of p does not propagate back to the ListInsert's caller. This happens because p is passed by value.
You need to turn int *p into int **p.

Resources