I don't know why the error is coming as mentioned in the title. The data are all numbers and the function reads it without problem when tested without pointers.
Note: I don't need to use 'malloc' or any other ones. I'm trying to figure what's going with the structure.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<limits.h>
#define MAX_POINTS 100
#define MAX_POLYGONS 100
typedef struct{
int polyid;
int npoints;
double x[MAX_POINTS];
//Stage1
double y[MAX_POINTS];
double perimeter;
double eccentricity;
double area;
} Polygon;
typedef struct {
Polygon npolygon[MAX_POLYGONS];
}polygons;
//function prototypes
void process_file(polygons *Total_poly);
int largestvalue_index(double A[],int m);
int largest_poly_id(double A[],int m, int B[]);
double area(double x[MAX_POINTS],double y[MAX_POINTS],int n);
double perimeter(double x[MAX_POINTS],double y[MAX_POINTS],int n);
double eccentricity(double area,double perimeter);
int main(int argc, char *argv[]) {
int i,j,k,l;
polygons Total_poly;
process_file(Total_poly);
printf("Stage 1\n");
printf("=====\n");
printf("First Polygon %d\n",Total_poly->npolygon[0]->polyid);
printf("x_val y_val\n");
for(i=0;i<Total_poly->npolygon[0].npoints;i++){
printf("%8.2f %8.2f\n",Total_poly->npolygon[0]->x[i],
Total_poly->npolygon[0]->y[i]);
}
printf("area=%.2f\n",area(Total_poly->npolygon[0].x,
Total_poly->npolygon[0]->y,Total_poly->npolygon[0]->npoints));
printf("perimeter=%.2f\n",perimeter(Total_poly->npolygon[0]->x,
Total_poly->npolygon[0]->y,Total_poly->npolygon[0]->npoints));
printf("eccentricity=%.2f\n",Total_poly->npolygon[0]->eccentricity);
//Stage2
printf("Stage 2");
printf("=======\n");
for(l=1;l<=5;l++){
printf("+-------");
}
printf("+\n");
printf("| id | nval | perim | area | eccen |\n");
for(l=1;l<=5;l++){
printf("+-------");
}
printf("+\n");
for(k=0;k<count;k++){
printf("| %5d | %5d |%6.2f |%6.2f |%6.2f |\n",
Total_poly->npolygon[k]->polyid,Total_poly->npolygon[k]->npoints,
Total_poly->npolygon[k]->perimeter,Total_poly->npolygon[k]->area,
Total_poly->npolygon[k]->eccentricity);
}
for(l=1;l<=5;l++){
printf("+-------");
}
//Stage3
return 0;
}
void process_file(polygons *Total_poly){
int count=0;
int i;
while(scanf("%d %d",&Total_poly.npolygon[count].npoints,
&Total_poly.npolygon[count].polyid)=2){
for(i=0;i<Total_poly.npolygon[count].npoints;i++){
if( scanf("%lf %lf",&Total_poly.npolygon[count].x[i],
&Total_poly.npolygon[count].y[i])=!2)
{
printf("Error");
exit(EXIT_FAILURE);
}else{
scanf("%lf %lf",&Total_poly.npolygon[count].x[i],
&Total_poly.npolygon[count].y[i]);
}
}
count++;
}
}
double area(double x[MAX_POINTS],double y[MAX_POINTS],int n){
int i,j;
double area=0;
j=n-1;
for(i=0;i<n;i++){
area+=(x[i]-x[j])*(y[i]+y[j]);
j=i;
}
return 0.5*fabs(area);
}
double perimeter(double x[MAX_POINTS],double y[MAX_POINTS],int n){
int i,j;
double length=0;
j=n-1;
for(i=0;i<n;i++){
length+=fabs(sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])));
j=i;
}
return length;
}
double eccentricity(double area,double perimeter){
return perimeter*perimeter/area/(4*M_PI);
}
int largest_poly_id(double A[],int m, int B[]){
int i,j;
int poly_index=0,largest = A[0];
for(i=1;i<m;i++){
if(A[i]>largest){
largest=A[i];
poly_index=i;
}
}
j=B[poly_index];
return j;
}
int largestvalue_index(double A[],int m){
int i;
int index=0,largest=A[0];
for(i=1;i<m;i++){
if (A[i]>largest){
largest=A[i];
index=i;
}
}
return index;
}
You are passing a struct to your process_file function. It is, however, expecting a pointer to a struct.
Change this line:
process_file(Total_poly);
to this:
process_file(&Total_poly);
Additionally, you'll need to change the -> operators in the printf statements in the main function to . operators.
Related
I have a problem with the compiler and I must have some logical mistakes (C language)
I want to have one void function that gives me the maximum price, the minimum price and the average of a table with maximum 100 prices. And if the user gives price=-1 I want the program to end!
Here is my code:
#include <stdio.h>
void function(float pin[],int j,float *min,float *max,float *mo,int cnt);
int main()
{
int i=0,count=0;
float prc[100],mo;
for(i=0;i<=99;i++)
{
printf("Enter price:");
scanf("%f",&prc[i]);
if(prc[i]==-1)
{
break;
}
count++;
}
int min=prc[0];
int max=prc[0];
void function(float prc,int i,float *min,float *max,float *mo,int count);
printf("Minimum price is:%f Maximum price is:%f and Mo is:%f",min,max,mo);
return 0;
}
void function(float pin[],int j,float *min,float *max,float *mo,int cnt)
{
float sum;
for(j=0;j<=cnt;j++)
{
if(pin[j]<*min)
{
min=pin[j];
}
if(pin[j]>*max)
{
*max=pin[j];
}
sum=+pin[j];
}
*mo=sum/j;
}
Looks like you have lots of little mistakes (missing dereference, using a variable out of scope, wrong operator, etc.):
#include <stdio.h>
void function(float pin[], float *min, float *max, float *mo, int cnt);
int main()
{
int i=0, count=0;
float prc[100], mo;
for (i=0; i<=99; i++)
{
printf("Enter price:");
scanf("%f", &prc[i]);
if (prc[i] == -1)
{
break;
}
count++;
}
float min=prc[0];
float max=prc[0];
function(&prc[0], &min, &max, &mo, count);
printf("Minimum price is:%f Maximum price is:%f and Mo is:%f", min, max, mo);
return 0;
}
void function(float pin[], float *min, float *max, float *mo, int cnt)
{
float sum = 0.0f;
for (int j=0; j < cnt; j++)
{
if (pin[j] < *min)
{
*min = pin[j];
}
if (pin[j] > *max)
{
*max = pin[j];
}
sum += pin[j];
}
*mo = sum / cnt;
}
There are a few mistakes in the function :
I have modified the code and looks to be fine except the function argument mo , decide whether to declare it as a float pointer or a float
Look at the declaration inside main, u are calling a function but the syntax is for declaration.
used mo as argument but mo is defined as float variable whereas the function accepts a pointer as per the declaration and there are a few warning related to the types
Declared ints but printing them using format specifiers for float %f
include
void function(float pin[],int j,float *min,float *max,float *mo,int cnt);
int main()
{
int i=0,count=0;
float prc[100],mo;
for(i=0;i<=99;i++)
{
printf("Enter price:");
scanf("%f",&prc[i]);
if(prc[i]==-1)
{
break;
}
count++;
}
int min=prc[0];
int max=prc[0];
function(prc,i,min,max,mo,count);
printf("Minimum price is:%f Maximum price is:%f and Mo is:%f",min,max,mo);
return 0;
}
void function(float pin[],int j,float *min,float *max,float *mo,int cnt)
{
float sum;
for(j=0;j<=cnt;j++)
{
if(pin[j]<*min)
{
*min=pin[j];
}
if(pin[j]>*max)
{
*max=pin[j];
}
sum=+pin[j];
}
*mo=sum/j;
}
wrong use of pointer and type,use
float min=prc[0];
float max=prc[0];
function(prc,i,&min,&max,&mo,count);
You declare as:
void function(float pin[],int j,float *min,float *max,float *mo,int cnt);
So you must deliver a float-pointer but not a float number.
whats the problem. It executes but doesnt show the correct answer.
It gets compiled as well.
I've used calling function in many places.
its assumed that the matrix is square and i give the input througgh terminal.
for ex=3
then a random 3x3 matrix but the value seems to be incorrect
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void row(int r1,int r2,int n,float A[n][n]){
int c;
float temp[n][n];
for(c=0;c<n;c++){
temp[r1][c]=A[r1][c];
A[r1][c]=A[r2][c];
A[r2][c]=temp[r1][c];
}
}
void maximum(int i, int n,float A[n][n]){
int j;
float max=fabs(A[i][i]);
for(j=i+1;j<n;j++){
if(fabs(A[j][i])>max){
max=fabs(A[j][i]);
row(i,j,n,A);
}
}
}
void op(int k, int n,float A[n][n]){
int i,j;
float f;f
for(i=1;i<n-1-k;i++){
f=-(A[k+i][k]/A[k][k]);
for(j=0;j<n-1-k;j++){
A[k+i][k+j]=A[k+i][k+j]+f*(A[k][k+j]);
}
}
}
int main(){
int i,j,n;
printf("Enter the order of the matrix:");
scanf("%d",&n);
float A[n][n];
for(i=0;i<n;i++){
for(j=0;j<n;j++)
scanf("%f",&A[i][j]);
}
float det=1;
for(i=0;i<n-1;i++){
maximum(i,n,A);
op(i,n,A);
det*=A[i][i];
}
det*=A[n-1][n-1];
printf("%f\n",det);
return 0;
}
I am trying to make a program that makes simple things. Actually, I know how to do it but in an easier way without pointers and stuff. However, I wondered how I could do it differently (like I did below). Obviously, there is something I miss about pointers, I did the math, but still I cannot get its philosophy.
Thank you!
long *read_array(int n1, int n2)
{
int i, j;
long a[n1][n2];
for(i=0;i<n1;i++)
for(j=0;j<n2;j++)
printf("Fill the table");
a[i][j]=GetLong();
return a;
}
long *Min_of_Rows(int m, int n, long *a)
{
long B[];
int i, j;
for(i=0;i<m;i++)
B[i]=a[i][0];
for(j=0;j<n;j++)
if (a[i][j]<B[i])
B[i]=a[i][j];
return B;
}
void *Print_B_array (int M, long *b)
{
int i;
for(i=0; i<M; i++)
printf("%ld\n",b[i]);
}
main()
{
long *a, *b;
int n1, n2;
printf("give rows");
n1=GetInteger();
printf("give columns");
n2=GetInteger();
a=read_array(n1, n2);
b=Min_of_rows(n1, n2, a);
Print_B_Array(n1, b);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
long *read_array(int n1, int n2){
int i, j;
long a[n1][n2];
printf("Fill the table\n");
for(i=0;i<n1;i++){
for(j=0;j<n2;j++){
a[i][j]=GetLong();
}
}
long *ret = malloc(sizeof(a));
memcpy(ret, a, sizeof(a));
return ret;
}
long *Min_of_Rows(int m, int n, long *a){
long *B = malloc(m*sizeof(*B));
int i, j;
long (*A)[n] = (void*)a;
for(i=0;i<m;i++){
B[i]=A[i][0];
for(j=0;j<n;j++){
if(A[i][j]<B[i])
B[i]=A[i][j];
}
}
return B;
}
void *Print_B_Array (int M, long *b){
int i;
for(i=0; i<M; i++)
printf("%ld\n", b[i]);
}
int main(void){
long *a, *b;
int n1, n2;
printf("give rows:");
n1=GetInteger();
printf("give columns:");
n2=GetInteger();
a=read_array(n1, n2);
b=Min_of_Rows(n1, n2, a);
printf("min of rows\n");
Print_B_Array(n1, b);
free(a);
free(b);
return 0;
}
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];
}
}
}
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.