You make suitable functions, that you are using pointers, that you find in handed realistic sequence (float)
largest and smallest element. If functions are successful, return address of largest or smallest element in array,
else return value NULL.
Write and test programm for test of both functions.
Reading of data must be adapted over suitable entry file, readout of results let goes on screen.
Programm must allow calculation of any number of tasks.
Here is my code:
And *.txt file:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2000 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
15 14 13 22 55 2 5
The program find min and max from all numbers in file, not only from numbers in first line, next from second line, and so on... to the EOF.
Where I am doing wrong ? Please, help me !
largest and smallest element. If functions are successful, return address of largest or smallest element in array,
else return value NULL.
Write and test programm for test of both functions.
Reading of data must be adapted over suitable entry file, readout of results let goes on screen.
Programm must allow calculation of any number of tasks.
Here is my code:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <iostream>
using namespace std;
#define MAXELEMENTS 100 // MAX numbers of elements
//function for max element in float array
float *biggest(float array[],int number)
{
int counter, counter_max;
float aretheysame=0;
for(counter=0; counter < number; counter++)
{
if (*(array+counter) > aretheysame)
{
aretheysame = *(array+counter);
counter_max = counter;
}
}
return(&*(array+counter_max));
}
//function for min element in float array
float *smallest(float array[],int number)
{
int counter, counter_max=0;
float aretheysame=array[0];
for(counter=0; counter < number; counter++)
{
if (*(array+counter) < aretheysame)
{
aretheysame = *(array+counter);
counter_max = counter;
}
}
return(&*(array+counter_max));
}
main(void)
{
FILE *in;
char inter[255];
int num_elements=0;
float element;
static float array[MAXELEMENTS];
float *small,*big;
printf("Adress and value of max and min element in array\n");
printf("Enter file name : ");
gets(inter);
in= fopen(inter,"r");
if(in == NULL)
{
printf("Error. No file with this name\n");
exit(1);
}
while (fscanf(in,"%f",&element) != EOF)
{
array[num_elements] = element;
num_elements++;
}
if (num_elements > 1)
{
big=biggest(array,num_elements);
printf("\nThe adress of max element is %d",&*big);
printf(", with value %f",*big);
small=smallest(array,num_elements);
printf("\nThe adress of min element is %d",&*small);
printf(", with value %f",*small);
}
else
{
;
}
fclose (in);
cin.ignore();
cin.get();
}
And *.txt file:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2000 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
15 14 13 22 55 2 5
The program find min and max from all numbers in file, not only from numbers in first line, next from second line, and so on... to the EOF.
Where I am doing wrong ? Please, help me !
