Reputation: 1779
This is my code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
int num_mezzo_1(int num_orig);
int num_mezzo_2(int num_orig);
int main(int argc, char *argv[]){
int num,contatore,tmp=0,tmp_1,tmp_2;
num=atoi(argv[1]);
if(num <= 3){
printf("%d è primo\n", num);
exit(0);
}
else{
num_mezzo_1(num);
num_mezzo_2(num);
tmp=tmp_1+tmp_2;
//using printf to debug
printf("t1 %d, t2 %d\n", tmp_1,tmp_2);
if(tmp>2){
printf("%d NON è primo\n", num);
}
else{
printf("%d è primo\n", num);
}
}
exit(0);
}
int num_mezzo_1(int num_orig){
int tmp_1=0,cont_1;
for(cont_1=1; cont_1<=(num_orig/2); cont_1++){
if((num_orig % cont_1) == 0){
tmp_1++;
}
}
//using printf to debug
printf("\n%d\n", tmp_1);
return tmp_1;
}
int num_mezzo_2(int num_orig){
int tmp_2=0,cont_2;
for(cont_2=((num_orig/2)+1); cont_2<=num_orig; cont_2++){
if((num_orig % cont_2) == 0){
tmp_2++;
}
}
//using printf to debug
printf("\n%d\n\n", tmp_2);
return tmp_2;
}
This program calculates wheter a number is prime or not.
If i give number 13 as input, the function num_1
has value 1
into tmp_1
and function num_2
has value 1
into tmp_2
and both are correct.
The problem is that tmp=tmp_1+tmp_2
return a big big big value and i don't understand why.
Upvotes: 0
Views: 10524
Reputation: 21351
You are calling the functions num_mezzo_1()
and num_mezzo_2()
but you are not storing their return values, so your variables tmp_1
and tmp_2
remain uninitialised.
Edit: Try changing the code
num_mezzo_1(num);
num_mezzo_2(num);
to
tmp_1 = num_mezzo_1(num);
tmp_2 = num_mezzo_2(num);
in the else block and see if you get what you expect.
Working code:
tmp=(num_mezzo_1(num)+num_mezzo_2(num));
Upvotes: 6