Read file, write to array, find min and max
-
//This program read file *.txt, than 100 float numbers(or int numbers, if they are present) //write in a float array.Next, finds MIN and MAX element in float array with using pointers, //write results on the screan and if there is no more numbers, go to end, else repet all again. #include #include #include #define MAXELEMENTS 100 // MAX numbers of elements //funkcion for max element in float array float *biggest(float table[],int number) { int counter, counter_max; float aretheysame=0; for(counter=0; counter < number; counter++) { if (*(table+counter) > aretheysame) { aretheysame = *(table+counter); counter_max = counter; } } return(&*(table+counter_max)); } //finkcion for min element in float array float *smallest(float table[],int number) { int counter, counter_max=0; float aretheysame=table[0]; for(counter=0; counter < number; counter++) { if (*(table+counter) < aretheysame) { aretheysame = *(table+counter); counter_max = counter; } } return(&*(table+counter_max)); } main(void) { FILE *in; char inter[255]; int num_elements=0; float element; static float table[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) { table[num_elements] = element; num_elements++; } if (num_elements > 1) { big=biggest(table,num_elements); printf("\nThe adress of max element is %d",&*big); printf(", with value %f",*big); small=smallest(table,num_elements); printf("\nThe adress of min element is %d",&*small); printf(", with value %f",*small); } else { ; } fclose (in); getchar(); } 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
-
//This program read file *.txt, than 100 float numbers(or int numbers, if they are present) //write in a float array.Next, finds MIN and MAX element in float array with using pointers, //write results on the screan and if there is no more numbers, go to end, else repet all again. #include #include #include #define MAXELEMENTS 100 // MAX numbers of elements //funkcion for max element in float array float *biggest(float table[],int number) { int counter, counter_max; float aretheysame=0; for(counter=0; counter < number; counter++) { if (*(table+counter) > aretheysame) { aretheysame = *(table+counter); counter_max = counter; } } return(&*(table+counter_max)); } //finkcion for min element in float array float *smallest(float table[],int number) { int counter, counter_max=0; float aretheysame=table[0]; for(counter=0; counter < number; counter++) { if (*(table+counter) < aretheysame) { aretheysame = *(table+counter); counter_max = counter; } } return(&*(table+counter_max)); } main(void) { FILE *in; char inter[255]; int num_elements=0; float element; static float table[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) { table[num_elements] = element; num_elements++; } if (num_elements > 1) { big=biggest(table,num_elements); printf("\nThe adress of max element is %d",&*big); printf(", with value %f",*big); small=smallest(table,num_elements); printf("\nThe adress of min element is %d",&*small); printf(", with value %f",*small); } else { ; } fclose (in); getchar(); } 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
If there are more than 100 numbers in the file, you'll need to change the value of
MAXELEMENTS
. A better solution would be to use a dynamic array. That way you do not have to keep changing and compiling code when the size of the input file changes.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
If there are more than 100 numbers in the file, you'll need to change the value of
MAXELEMENTS
. A better solution would be to use a dynamic array. That way you do not have to keep changing and compiling code when the size of the input file changes.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
//This program read file *.txt, than 100 float numbers(or int numbers, if they are present) //write in a float array.Next, finds MIN and MAX element in float array with using pointers, //write results on the screan and if there is no more numbers, go to end, else repet all again. #include #include #include #define MAXELEMENTS 100 // MAX numbers of elements //funkcion for max element in float array float *biggest(float table[],int number) { int counter, counter_max; float aretheysame=0; for(counter=0; counter < number; counter++) { if (*(table+counter) > aretheysame) { aretheysame = *(table+counter); counter_max = counter; } } return(&*(table+counter_max)); } //finkcion for min element in float array float *smallest(float table[],int number) { int counter, counter_max=0; float aretheysame=table[0]; for(counter=0; counter < number; counter++) { if (*(table+counter) < aretheysame) { aretheysame = *(table+counter); counter_max = counter; } } return(&*(table+counter_max)); } main(void) { FILE *in; char inter[255]; int num_elements=0; float element; static float table[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) { table[num_elements] = element; num_elements++; } if (num_elements > 1) { big=biggest(table,num_elements); printf("\nThe adress of max element is %d",&*big); printf(", with value %f",*big); small=smallest(table,num_elements); printf("\nThe adress of min element is %d",&*small); printf(", with value %f",*small); } else { ; } fclose (in); getchar(); } 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
I'm having trouble trying to understand what your problem is. You want to read all numbers and get the largest and smallest? I'm not understanding what you say is going wrong. I would try something like this: #include #include using namespace std; set nums; ifstream in(filename); while(in) { int n; in >> n; nums.insert(n); } set::iterator it = nums.begin(); cout << "first = " << (*it) << endl; int last; while(it != nums.end()) { last = (*it); it++; } cout << "last = " << last << endl; -- Rocky Dean Pulley
-
I'm having trouble trying to understand what your problem is. You want to read all numbers and get the largest and smallest? I'm not understanding what you say is going wrong. I would try something like this: #include #include using namespace std; set nums; ifstream in(filename); while(in) { int n; in >> n; nums.insert(n); } set::iterator it = nums.begin(); cout << "first = " << (*it) << endl; int last; while(it != nums.end()) { last = (*it); it++; } cout << "last = " << last << endl; -- Rocky Dean Pulley
-
I want to read all numbers and get the largest and smallest, but only from first 100 numbers in array[100], after that the same with another 100 numbers and so on.
As you are reading in the numbers, if
num_elements
is a multiple of 100, then callbiggest()
andsmallest()
. Another way would be to read all of the numbers in the file, and call thebiggest()
andsmallest()
functions once for each group of 100 numbers, like:big1 = biggest(table, 100); // search the first 100 numbers
big2 = biggest(&table[100], 100); // search the second 100 numbers
big3 = biggest(&table[200], 100); // search the third 100 numbersMake sense?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
//This program read file *.txt, than 100 float numbers(or int numbers, if they are present) //write in a float array.Next, finds MIN and MAX element in float array with using pointers, //write results on the screan and if there is no more numbers, go to end, else repet all again. #include #include #include #define MAXELEMENTS 100 // MAX numbers of elements //funkcion for max element in float array float *biggest(float table[],int number) { int counter, counter_max; float aretheysame=0; for(counter=0; counter < number; counter++) { if (*(table+counter) > aretheysame) { aretheysame = *(table+counter); counter_max = counter; } } return(&*(table+counter_max)); } //finkcion for min element in float array float *smallest(float table[],int number) { int counter, counter_max=0; float aretheysame=table[0]; for(counter=0; counter < number; counter++) { if (*(table+counter) < aretheysame) { aretheysame = *(table+counter); counter_max = counter; } } return(&*(table+counter_max)); } main(void) { FILE *in; char inter[255]; int num_elements=0; float element; static float table[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) { table[num_elements] = element; num_elements++; } if (num_elements > 1) { big=biggest(table,num_elements); printf("\nThe adress of max element is %d",&*big); printf(", with value %f",*big); small=smallest(table,num_elements); printf("\nThe adress of min element is %d",&*small); printf(", with value %f",*small); } else { ; } fclose (in); getchar(); } 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
-
If not using STL at all is an option, you might ask yourself why you store the numbers in an array in the first place. Just keep track of the smallest and largest numbers you fscanf'ed so far.
it sounds like he has to for a homework assignment. It sounds like a pretty lame homework assignment, must have been a math teacher turned programming teacher. -- Rocky Dean Pulley
-
As you are reading in the numbers, if
num_elements
is a multiple of 100, then callbiggest()
andsmallest()
. Another way would be to read all of the numbers in the file, and call thebiggest()
andsmallest()
functions once for each group of 100 numbers, like:big1 = biggest(table, 100); // search the first 100 numbers
big2 = biggest(&table[100], 100); // search the second 100 numbers
big3 = biggest(&table[200], 100); // search the third 100 numbersMake sense?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
it sounds like he has to for a homework assignment. It sounds like a pretty lame homework assignment, must have been a math teacher turned programming teacher. -- Rocky Dean Pulley
-
I can't really do your homework for you, you won't learn anything that way. Besides, you are 90% there, just take the time and figure it out, you're almost done. -- Rocky Dean Pulley
-
I can't really do your homework for you, you won't learn anything that way. Besides, you are 90% there, just take the time and figure it out, you're almost done. -- Rocky Dean Pulley
-
Yes, but how can i write big10489=biggest(&table[1048800], 100); ? This will take a lot of my time. And i don?t now, if file is so long?
dr.eu wrote: Yes, but how can i write big10489=biggest(&table[1048800], 100); ? Exactly like that. This assumes that you have read at least 1,048,900 numbers from the file.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
Yes, from that almost i am gona crazy. The result is O.K. (for all numbers in file). Help me please with 100, 200, ... elements in array !!!
just use the same array, once you hit a number where (current_number % 100 == 0) then do your biggest/smallest check and reset current_number to 0, keep going on after that and it will just reuse your 100 item array. -- Rocky Dean Pulley
-
dr.eu wrote: Yes, but how can i write big10489=biggest(&table[1048800], 100); ? Exactly like that. This assumes that you have read at least 1,048,900 numbers from the file.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
just use the same array, once you hit a number where (current_number % 100 == 0) then do your biggest/smallest check and reset current_number to 0, keep going on after that and it will just reuse your 100 item array. -- Rocky Dean Pulley
-
I done so: while (fscanf(in,"%f",&element) != EOF) { table[num_elements] = element; current_number=element; current_number%100==0; num_elements++; } but steel dont work propertly.
How about:
while (...)
{
table[num_elements] = element;
num_elements++;
if ((num_elements % 100) == 0)
{
biggest(...);
smallest(...);
num_elements = 0;
}
}
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
How about:
while (...)
{
table[num_elements] = element;
num_elements++;
if ((num_elements % 100) == 0)
{
biggest(...);
smallest(...);
num_elements = 0;
}
}
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
I try, but compiler return erors: too few arguments to function `float * biggest(float *, int)' at this point in file too few arguments to function `float * smallest(float *, int)' at this point in file
I don't mean to sound mean but you should really be able to figure that one out. -- Rocky Dean Pulley
-
I don't mean to sound mean but you should really be able to figure that one out. -- Rocky Dean Pulley