I have an array and I have to find the largest element in it, tell how many times it is in and in what position. I have to use pointers and dynamic memory allocation.
This code works properly:
#include <stdio.h>
#include <stdlib.h>
void max_elem (int n, float* vett) {
int i=0, *pos, p=0, n_ele_pos;
float max=*(vett);
pos=(int*)malloc(sizeof(int));
if (pos==NULL) {printf("BUM\n" ); exit (0);}
*(pos)=0;
for (i=1; i<n; i++) {
if (*(vett+i)>max) {
max=*(vett+i);
p=0;
*(pos)=i;
}
else if (*(vett+i)==max) {
p++;
*(pos+p)=i;
}
}
if (p==0) {
printf("\nmax element is %f, in position %d ", max, *pos+1);
}
else {
printf("\n max element %f, %d times in position\n", max, p+1);
for (i=0; i<p+1; i++) {
printf("%d ", *(pos+i)+1);
}
}
printf("\n\n");
free(pos);
}
int main()
{
int i,n;
float *element;
printf("\n\n Pointer : Find the largest element using Dynamic Memory Allocation :\n");
printf("-------------------------------------------------------------------------\n");
printf(" Input total number of elements(1 to 100): ");
scanf("%d",&n);
element=(float*)calloc(n,sizeof(float)); // Memory is allocated for 'n' elements
if(element==NULL)
{
printf(" No memory is allocated.");
exit(0);
}
printf("\n");
for(i=0;i<n;++i)
{
printf(" Number %d: ",i+1);
scanf("%f",element+i);
}
max_elem (n, element);
return 0;
}
but the next one doesn't. I have to fill the array with random numbers in a specific range decided by me, even from decimal to decimal extrems for integers random numbers (yes, I know it doesn't make much sense practically). Search when filled with decimal numbers works fine sometimes, with integers I would have just 0s.
Here the code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
float rand_float (float *, float *);
int rand_int (float *, float *);
void min_max(float *, float *, float *, float *);
int num_intero(float);
void max_elem (int, float*);
int main () {
int n, i, t;
float x1, x2;
printf ("array size:\t");
scanf ("%d", &n);
printf("\ninterval extremes:\t");
scanf ("%f %f", &x1, &x2);
do {
printf("1 for decimal random numbers, 2 for integers:\t");
scanf("%d", &t);
} while (t!=1 && t!=2);
srand(time(NULL));
switch (t) {
case 1 : {
float *vett;
vett=(float*)calloc(n, sizeof(float));
if (vett==NULL) {printf("BUM\n" ); exit (0);}
for (i=0;i<n;i++) {
*(vett+i)=rand_float(&x1, &x2);
printf("%d__\t%10f\n", i, *(vett+i));
}
max_elem (n, vett);
free(vett);
break;
}
case 2 : {
int *vett;
vett=(int*)calloc(n, sizeof(int));
if (vett==NULL) {printf("BUM\n" ); exit (0);}
for (i=0; i<n; i++) {
*(vett+i)=rand_int(&x1, &x2);
printf("%d__\t%10d\n", i, *(vett+i));
}
max_elem (n, (float*)vett);
free (vett);
break;
}
}
return 0;
}
void min_max (float*x1, float *x2, float *min, float *max) {
if (*x1<*x2) {
*min=*x1;
*max=*x2;
}
else {
*min=*x2;
*max=*x1;
}
}
float rand_float (float *x1, float *x2) {
float min, max;
min_max(x1, x2, &min, &max);
return ((max-min)*(float)rand()/RAND_MAX)+min;
}
int num_intero (float min) {
if (min/(int)min==1)
return 1; //e' intero
else return 0;
}
int rand_int (float *x1, float *x2) {
float min, max;
min_max(x1, x2, &min, &max);
if ((int)min==(int)max) {
return (int)min;
}
else if (min==0) {
return (rand()%((int)(max)+1));
}
else if (max==0) {
return (rand()%((int)(-min)+1)+min); //funziona anche con ((int)(min)-1)+min
}
else if ((int)min!=(int)max) {
if (num_intero (min)==1) {
return (rand()%((int)(max-min)+1)+min);
}
else if (num_intero (max)==0) {
return (rand()%((int)(max-min))+min+1);
}
else return (rand()%((int)(max-min)+1)+min+1);
}
}
void max_elem (int n, float* vett) {
int i=0, *pos, p=0, n_ele_pos;
float max=*(vett);
pos=(int*)malloc(sizeof(int));
if (pos==NULL) {printf("BUM\n" ); exit (0);}
*(pos)=0;
for (i=1; i<n; i++) {
if (*(vett+i)>max) {
max=*(vett+i);
p=0;
*(pos)=i;
}
else if (*(vett+i)==max) {
p++;
*(pos+p)=i;
}
}
if (p==0) {
printf("\nmax element is %f, in position %d ", max, *pos+1);
}
else {
printf("\n max element %f, %d times in position\n", max, p+1);
for (i=0; i<p+1; i++) {
printf("%d ", *(pos+i)+1);
}
}
printf("\n\n");
free(pos);
}
In addition, when the array is big I have runtime error like this, that I don't have with the first code: error
Example: first code, second code and integers error(bottom)
I'm on Ubuntu 16.04 (gnome) with gcc and atom editor. Thanks and sorry.
Aucun commentaire:
Enregistrer un commentaire