Using pthreads and the code doesn't even get to main method - c

I am doing an assignment for school and I am supposed to make the following code (which generates pgm image with a Buddhabrot fractal) parallel.
Thing is the compiler doesn't point any errors and still the code won't even get to main. I have already tried looking it up but didn't find a way to solve it.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#define NT 3
int nColumns, nLines, size, ite, proximo;
double dt, x, y;
float **mat;
pthread_mutex_t mutex;
pthread_cond_t condM;
pthread_cond_t condE;
typedef struct {
int x;
int y;
} int2;
typedef struct{
double x;
double y;
} double2;
int2 coordinatesConversion(double x,double y, int nColumns,int nLines){
int2 ret;
int2 retError;
retError.x=-1;
retError.y=-1;
ret.x=round(((2.0+x)/3.5) *((double)(nColumns-1)));
ret.y=round(((1.5+y)/3.5) *((double)(nLines-1)));
if(ret.x<0 || ret.x>=nColumns) return retError;
if(ret.y<0 || ret.y>=nLines) return retError;
return ret;
}
int printMatrixToFilePGM(float **mat,int tamx, int nLines, char *srcFile){
FILE *arq=fopen(srcFile,"w");
int cont, cont2;
float min,max;
min=mat[0][0];
max=mat[0][0];
for(cont=0;cont<nLines;cont++)
for(cont2=0;cont2<tamx;cont2++){
if(min>mat[cont][cont2]) min=mat[cont][cont2];
if(max<mat[cont][cont2]) max=mat[cont][cont2];
}
max=max*0.35;
float delta = max-min;
fprintf(arq,"P2 \n");
fprintf(arq,"%d\n%d \n",tamx,nLines);
fprintf(arq,"255\n");
for(cont=0;cont<nLines;cont++)
for(cont2=0;cont2<tamx;cont2++){
int valpixel=((mat[cont][cont2]-min)/delta)*255.0f;
if(valpixel>255) valpixel=255;
fprintf(arq,"%d \n", valpixel);
}
fclose(arq);
}
float** mallocFloatMatrix(int tamx, int nLines, float defaultValueOfTheElementsAtMatrix){
float **errorCodeReturn=0x0;
float **mat;
int i,j;
int condErrorMalloc=0;
mat=malloc(sizeof(float *)*nLines);
if(mat==0x0) return errorCodeReturn;
for(i=0;i<tamx;i++) mat[i]=malloc(sizeof(float )*tamx);
for(i=0;i<tamx;i++)
if(mat[i]==0x0){
condErrorMalloc=1;
break;
}
if(condErrorMalloc==0) return mat;
for(i=0;i<nLines;i++) for(j=0;j<tamx;j++) mat[i][j]=defaultValueOfTheElementsAtMatrix;
for(i=0;i<tamx;i++) if(mat[i]!=0x0) free(mat[i]);
free(mat);
return errorCodeReturn;
}
void freeFloatMatrix(float **mat,int tamx, int nLines){
int i;
for(i=0;i<nLines;i++) if(mat[i]!=0x0) free(mat[i]);
free(mat);
}
int iteration(double x,double y, int nColumns,int nLines, int ite,int2 *iterationPath){
int cont;
int condInvalidPointer=1;
double2 z;
z.x=0.0;
z.y=0.0;
double2 c;
c.x=x;
c.y=y;
double2 zt;
for(cont=0;cont<ite;cont++){
zt.x=((z.x*z.x)-(z.y*z.y))+c.x;
zt.y=(2.0*(z.x*z.y))+c.y;
z=zt;
if(((z.x*z.x)+(z.y*z.y))>4.0){
if(cont>100) condInvalidPointer=0;
break;
}
iterationPath[cont]=coordinatesConversion(z.x,z.y,nColumns,nLines);
}
if(condInvalidPointer) return 0;
return cont;
}
void *mestre(void *param) {
int i;
for(i=0;i<size;i++){
x = -2.0+((double)i*dt);
for(y=-2.0;y<2.0;y=y+dt){
pthread_mutex_lock(&mutex);
proximo++;
pthread_cond_signal(&condE);
pthread_cond_wait(&condM, &mutex);
pthread_mutex_unlock(&mutex);
}
}
}
void *escravo(void *param) {
pthread_mutex_lock(&mutex);
if(proximo==0) pthread_cond_wait(&condE, &mutex);
double xe = x;
double ye = y;
proximo--;
pthread_cond_signal(&condM);
pthread_mutex_unlock(&mutex);
int k;
int2* iterationPath = (int2 *)malloc(sizeof(int2)*ite);
if(iterationPath==0x0) return 0x0;
int completedIterations = iteration(xe, ye, nColumns, nLines, ite, iterationPath);
for(k=0;k<completedIterations;k++){
if(iterationPath[k].x!=-1 && iterationPath[k].y!=-1)
mat[iterationPath[k].x][iterationPath[k].y]=mat[iterationPath[k].x][iterationPath[k].y]+1.0f;
}
free(iterationPath);
}
int main(void){
printf("Main method has started.");
pthread_mutex_init(&(mutex), NULL);
pthread_cond_init(&(condM), NULL);
pthread_cond_init(&(condE), NULL);
nColumns=2048;
nLines=2048;
dt=0.001;
size=round(4.0/dt);
ite=600;
proximo = 0;
mat=mallocFloatMatrix(nColumns,nLines,0.0f);
if(mat==0x0) return 0;
pthread_t m;
pthread_t escravos[NT-1];
pthread_create(&m, NULL, mestre, (void*)0);
int i;
for(i=0; i<NT-1; i++) pthread_create(&escravos[i], NULL, escravo, (void*)0);
pthread_exit(NULL);
printMatrixToFilePGM(mat,nColumns,nLines,"saida5.pgm");
freeFloatMatrix(mat,nColumns,nLines);
return 0;
}

Related

invalid operands to binary + c programming

Hi I'm finding a sum of polynomial in c, without massive, and i have this error that says " invalid operands to binary+(have 'float()(int, int, int)' and 'float()(int, int, int)'"
here is the code
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float p6(int p6, int x, int a){ `function to find pow`
p6=pow(x, 6);
p6=a*p6;
return p6;
}
float p5(int p5, int x, int a){ `small function`
p5=pow(x, 5);
p5=p5*a;
return p5;
}
float p4(int p4, int x, int a){
p4=pow(x, 4);
p4=a*p4;
return p4;
}
float p3(int p3, int a, int x){
p3=pow(x, 3);
p3=a*p3;
return p3;
}
float p2(int p2, int a, int x){
p2=pow(x, 2);
p2=a*p2;
return p2;
}
main (){ `main function starts here`
int i, a;
double sum=0;
float x;
printf("x-iin utgiig oruul"); `value of x`
scanf("%lf", &x);
printf("a1-a6 toog oruul"); `value of coefficents`
for(i=1; i<=6; i++){ `for coeffincents`
scanf("%d", &a);
}
sum=p6+p5+p4+p3+p2+a*x; `error occurs here`
printf("%d", sum);
system("pause");
return 0;
}
baceause p6, p5, ... are functions so you you have to make function calls in the main. Somthing like that:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float p6(int x, int a){ //`function to find pow`
int p6=pow(x, 6);
p6=a*p6;
return p6;
}
float p5(int x, int a){ //`small function`
int p5=pow(x, 5);
p5=p5*a;
return p5;
}
float p4(int x, int a){
int p4=pow(x, 4);
p4=a*p4;
return p4;
}
float p3(int a, int x){
int p3=pow(x, 3);
p3=a*p3;
return p3;
}
float p2(int a, int x){
int p2=pow(x, 2);
p2=a*p2;
return p2;
}
int main (){ //`main function starts here`
int i, a;
double sum=0;
float x;
printf("x-iin utgiig oruul"); //`value of x`
scanf("%f", &x);
printf("a1-a6 toog oruul"); //`value of coefficents`
for(i=1; i<=6; i++){ //`for coeffincents`
scanf("%d", &a);
}
sum=p6(x, a)+p5(x, a)+p4(x, a)+p3(x, a)+p2(x, a)+a*x; //`error occurs here`
printf("%lf", sum);
system("pause");
return 0;
}

Function average and stdDev const int tab[ ]. Average problems

I have to use:
float average(const int tab[], int size);
float stdDev(const int tab[], int size);
to printf average and stdDev in C.
I have problem with average and i think with const int.
When i add const int tab[101] i have error with a1;
So how can i make it work with const int (if i can).
And if it is anything wrong with this code.
Any help will be helpful.
#include<stdio.h>
#include<math.h>
float average(const int tab[], int size);
float stdDev(const int tab[], int size);
int main()
{
float ave, std;
int a1;
int j;
int tab[101];
printf("Podaj liczby: ");
for(j=0; j<=99; j++)
{
a1 = scanf("%d", &tab[j]);
if(a1<1)
{
printf("Incorrect input");
return 1;
}
if(tab[0]==0)
{
printf("not enough data available");
return 2;
}
if(tab[j]==0)
{
break;
}
}
ave = average(tab, j);
printf("%.2f\n", ave);
std = stdDev(tab, j);
printf("%.2f", std);
return 0;
}
float average(const int tab[], int size)
{
int i;
float y=0, x;
if(size<=0)
{
return -1;
}
for(i=0; i<size; i++)
{
x = x + tab[i];
}
y = x/size;
return y;
}
float stdDev(const int tab[], int size)
{
int i;
float y, z, z1, z2=0, z3=0;
if(size<=0)
{
return -1;
}
y = average(tab, size);
for(i=0; i<size; i++)
{
z = tab[i] - y;
z1 = pow(z, 2);
z2 = z2 + z1;
z=0;
z1=0;
}
z3 = sqrt(z2/size);
return z3;
}
You define the variable x in average here:
float y=0, x;
without giving it a value. Then here:
x = x + tab[i];
you are reading its value without setting it anywhere beforehand. Because you never gave x a value, its value will be indeterminate and reading it will cause undefined behavior, which means that your program could e.g. print garbage output.
Always initialize your variables:
float y=0, x=0;

[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

expected 'struct polygons *' but argument is of

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.

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