dr.eu
March 18th, 2005, 04:54 PM
Please, help me, is urgent !
Make suitable functions, that are using pointers, that find in handed realistic sequence (float)
largest and smallest element. If functions are successful, return address and value of largest
and 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:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <iostream>
using namespace std;
#define MAXELEMENTS 1000 // 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;
cout << "Adress and value of max and min element in array " << endl;
cout << "Enter file name : ";
gets(inter);
in= fopen(inter,"r");
if(in == NULL)
{
cout << "Error. No file with this name" << endl;
exit(1);
}
while (fscanf(in,"%f",&element) != EOF)
{
array[num_elements] = element;
num_elements++;
}
if (num_elements > 1)
{
big=biggest(array,num_elements);
cout << endl << "The adress of max element is " << (int)big << ", with value " << *big;
small=smallest(array,num_elements);
cout << endl << "The adress of min element is " << (int)small << ", with value " << *small;
}
else
{
cout << endl << endl << "Error in data file ! ";
}
fclose (in);
cin.ignore();
}
And test1.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 200 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...
Every line must be task forself.
The result of output must be like:
Adress and value of max and min element in array
Enter file name : test1.txt
1. task:
The adress of max element is 4420144, with value 100.000000
The adress of min element is 4420020, with value 0.000000
2. task:
The adress of max element is 4420144, with value 200.000000
The adress of min element is 4420020, with value -10.000000
3. task:
The adress of max element is 4420144, with value 55.000000
The adress of min element is 4420020, with value 2.000000
and so on ....
Where I am doing wrong ? Please, help me !
Make suitable functions, that are using pointers, that find in handed realistic sequence (float)
largest and smallest element. If functions are successful, return address and value of largest
and 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:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <iostream>
using namespace std;
#define MAXELEMENTS 1000 // 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;
cout << "Adress and value of max and min element in array " << endl;
cout << "Enter file name : ";
gets(inter);
in= fopen(inter,"r");
if(in == NULL)
{
cout << "Error. No file with this name" << endl;
exit(1);
}
while (fscanf(in,"%f",&element) != EOF)
{
array[num_elements] = element;
num_elements++;
}
if (num_elements > 1)
{
big=biggest(array,num_elements);
cout << endl << "The adress of max element is " << (int)big << ", with value " << *big;
small=smallest(array,num_elements);
cout << endl << "The adress of min element is " << (int)small << ", with value " << *small;
}
else
{
cout << endl << endl << "Error in data file ! ";
}
fclose (in);
cin.ignore();
}
And test1.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 200 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...
Every line must be task forself.
The result of output must be like:
Adress and value of max and min element in array
Enter file name : test1.txt
1. task:
The adress of max element is 4420144, with value 100.000000
The adress of min element is 4420020, with value 0.000000
2. task:
The adress of max element is 4420144, with value 200.000000
The adress of min element is 4420020, with value -10.000000
3. task:
The adress of max element is 4420144, with value 55.000000
The adress of min element is 4420020, with value 2.000000
and so on ....
Where I am doing wrong ? Please, help me !