Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Read file, write to array, find min and max

Read file, write to array, find min and max

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelp
20 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R rocky_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

    D Offline
    D Offline
    dr eu
    wrote on last edited by
    #5

    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.

    D 1 Reply Last reply
    0
    • D dr eu

      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.

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #6

      As you are reading in the numbers, if num_elements is a multiple of 100, then call biggest() and smallest(). Another way would be to read all of the numbers in the file, and call the biggest() and smallest() 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 numbers

      Make sense?


      "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

      D 1 Reply Last reply
      0
      • D dr eu

        //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

        M Offline
        M Offline
        Mattias G
        wrote on last edited by
        #7

        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.

        R 1 Reply Last reply
        0
        • M Mattias G

          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.

          R Offline
          R Offline
          rocky_pulley
          wrote on last edited by
          #8

          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

          D 1 Reply Last reply
          0
          • D David Crow

            As you are reading in the numbers, if num_elements is a multiple of 100, then call biggest() and smallest(). Another way would be to read all of the numbers in the file, and call the biggest() and smallest() 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 numbers

            Make sense?


            "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

            D Offline
            D Offline
            dr eu
            wrote on last edited by
            #9

            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?

            D 1 Reply Last reply
            0
            • R rocky_pulley

              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

              D Offline
              D Offline
              dr eu
              wrote on last edited by
              #10

              About homework you have right ! About teacher ... Who nows ? Any way could you help ?

              R 1 Reply Last reply
              0
              • D dr eu

                About homework you have right ! About teacher ... Who nows ? Any way could you help ?

                R Offline
                R Offline
                rocky_pulley
                wrote on last edited by
                #11

                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

                D 1 Reply Last reply
                0
                • R rocky_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

                  D Offline
                  D Offline
                  dr eu
                  wrote on last edited by
                  #12

                  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 !!!

                  R 1 Reply Last reply
                  0
                  • D dr eu

                    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?

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #13

                    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

                    D 1 Reply Last reply
                    0
                    • D dr eu

                      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 !!!

                      R Offline
                      R Offline
                      rocky_pulley
                      wrote on last edited by
                      #14

                      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

                      D 1 Reply Last reply
                      0
                      • D David Crow

                        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

                        D Offline
                        D Offline
                        dr eu
                        wrote on last edited by
                        #15

                        In that case, i have to import another counter j: int j; bigj=biggest(&table[j], 100); Is right so?

                        1 Reply Last reply
                        0
                        • R rocky_pulley

                          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

                          D Offline
                          D Offline
                          dr eu
                          wrote on last edited by
                          #16

                          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.

                          D 1 Reply Last reply
                          0
                          • D dr eu

                            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.

                            D Offline
                            D Offline
                            David Crow
                            wrote on last edited by
                            #17

                            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

                            D 1 Reply Last reply
                            0
                            • D 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

                              D Offline
                              D Offline
                              dr eu
                              wrote on last edited by
                              #18

                              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

                              R 1 Reply Last reply
                              0
                              • D dr eu

                                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

                                R Offline
                                R Offline
                                rocky_pulley
                                wrote on last edited by
                                #19

                                I don't mean to sound mean but you should really be able to figure that one out. -- Rocky Dean Pulley

                                D 1 Reply Last reply
                                0
                                • R rocky_pulley

                                  I don't mean to sound mean but you should really be able to figure that one out. -- Rocky Dean Pulley

                                  D Offline
                                  D Offline
                                  dr eu
                                  wrote on last edited by
                                  #20

                                  O.K., Thank you anyway for yours great help and good understanding. I'll try to do it well alone, but i'm not sure in access. Thanks again and goodbay !

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups