Array-related Functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brady
    New Member
    • Mar 2007
    • 59

    #1

    Array-related Functions

    i have a function called showArray(int ar[], int numElements, and int numPerLine)
    i am having some issues with creating the function properly so that the caller can call it. so far i have this:

    /*************** ***************
    Function Name: void showArray(int ar[], int numElements, int numPerLine)



    *************** ***************/
    void showArray(int ar[], int numElements, int numPerLine)
    {
    int i;`


    while(numElemen ts < 200)
    {
    i = ar[0];
    cout << i;
    i++;
    }
    if(numPerLine *= 2)
    cout << endl;


    an array will be passed through which has 200 elements. in main i have a symbolic const for the number of elements but dont know what passes through the other two arguments. any suggestions?!?! i know the function is coded incorrectly also! Thanks
  • brady
    New Member
    • Mar 2007
    • 59

    #2
    By the way, the function needs to display the elements of the array, several item per line as specified by the third argument. The seconds argument tells the function how many items are in the array, so it knows where to stop. i need to output a newline character when the number of items slready printed is a multiple of numPerLines. maybe the % operator? Thanks

    Comment

    • Savage
      Recognized Expert Top Contributor
      • Feb 2007
      • 1759

      #3
      Hi brady,

      You have not defined a good condition for your while loop.You cannot predict
      that your array will have 200 elements.Why not use instead something like:

      Code:
      int i=0;
      int element
      
      while(i<numElements)
      {
         element=ar[i];
         ar[i-1]='\0';
         cout<<element;
         i++;
      }
      Hmm,for third argument you could integrate inside loop a counter which will count up to numPerLine and then it must become counter=counter %numPerLine and cout \n character to force the start of a new line.


      Savage

      Comment

      • brady
        New Member
        • Mar 2007
        • 59

        #4
        Hello Savage,
        Thanks for the reply! That makes a lot more sense then what i had. For the counter, can i just place that right after incrementing i? then maybe have a decision statement. im new with C++ so its a bit confusing.

        Originally posted by Savage
        Hi brady,

        You have not defined a good condition for your while loop.You cannot predict
        that your array will have 200 elements.Why not use instead something like:

        Code:
        int i=0;
        int element
        
        while(i<numElements)
        {
           element=ar[i];
           ar[i-1]='\0';
           cout<<element;
           i++;
        }
        Hmm,for third argument you could integrate inside loop a counter which will count up to numPerLine and then it must become counter=counter %numPerLine and cout \n character to force the start of a new line.


        Savage

        Comment

        • Savage
          Recognized Expert Top Contributor
          • Feb 2007
          • 1759

          #5
          Originally posted by brady
          Hello Savage,
          Thanks for the reply! That makes a lot more sense then what i had. For the counter, can i just place that right after incrementing i? then maybe have a decision statement. im new with C++ so its a bit confusing.

          Yes,that should do it!!!

          Savage

          Comment

          • brady
            New Member
            • Mar 2007
            • 59

            #6
            Another question, is the '\0' a null value?

            Originally posted by Savage
            Hi brady,

            You have not defined a good condition for your while loop.You cannot predict
            that your array will have 200 elements.Why not use instead something like:

            Code:
            int i=0;
            int element
            
            while(i<numElements)
            {
               element=ar[i];
               ar[i-1]='\0';
               cout<<element;
               i++;
            }
            Hmm,for third argument you could integrate inside loop a counter which will count up to numPerLine and then it must become counter=counter %numPerLine and cout \n character to force the start of a new line.


            Savage

            Comment

            • Savage
              Recognized Expert Top Contributor
              • Feb 2007
              • 1759

              #7
              No,it's a terminating character which will ensure that you don't acsidently
              cout previos element.


              Savage

              Comment

              • brady
                New Member
                • Mar 2007
                • 59

                #8
                Yes thats right! Thanks for the refresher and thanks for your help! This function was driving me crazy! i'll post the final here in a minute and if you're around and happen to view the post and see a mistake could you let me know? i cant test it yet because im having issues passing arguments into the function. im getting the invalid conversion to int*, int or something like that

                Originally posted by Savage
                No,it's a terminating character which will ensure that you don't acsidently
                cout previos element.


                Savage

                Comment

                • Savage
                  Recognized Expert Top Contributor
                  • Feb 2007
                  • 1759

                  #9
                  Originally posted by brady
                  Yes thats right! Thanks for the refresher and thanks for your help! This function was driving me crazy! i'll post the final here in a minute and if you're around and happen to view the post and see a mistake could you let me know? i cant test it yet because im having issues passing arguments into the function. im getting the invalid conversion to int*, int or something like that
                  Please post just the troubleshooting part because if you post full code it will
                  be snipped by the moderators(see posting guidelines)

                  Savage

                  Comment

                  • brady
                    New Member
                    • Mar 2007
                    • 59

                    #10
                    I didnt know that. and yes i will follow those guidlines. thanks for the fore warning

                    Originally posted by Savage
                    Please post just the troubleshooting part because if you post full code it will
                    be snipped by the moderators(see posting guidelines)

                    Savage

                    Comment

                    • Savage
                      Recognized Expert Top Contributor
                      • Feb 2007
                      • 1759

                      #11
                      Originally posted by brady
                      I didnt know that. and yes i will follow those guidlines. thanks for the fore warning
                      Always a pleasure..


                      Savage

                      Comment

                      • brady
                        New Member
                        • Mar 2007
                        • 59

                        #12
                        Well i received a successful build after implementing the counter. just hope it works. here is what i coded after i was incremented:
                        while....
                        //body of loop previously posted
                        counter++;
                        if(counter = counter%numPerL ine)
                        cout << "/n";

                        Originally posted by Savage
                        Always a pleasure..


                        Savage

                        Comment

                        • Savage
                          Recognized Expert Top Contributor
                          • Feb 2007
                          • 1759

                          #13
                          Originally posted by brady
                          Well i received a successful build after implementing the counter. just hope it works. here is what i coded after i was incremented:
                          while....
                          //body of loop previously posted
                          counter++;
                          if(counter = counter%numPerL ine)
                          cout << "/n";
                          Here I can find 2 errors.

                          FIRST:


                          Code:
                          if(counter = counter%numPerLine)
                          is invalid.This condition will never become TRUE.

                          try using instead

                          Code:
                          if(counter==numPerLine) counter=counter%numPerLine,cout <<"\n";
                          SECOUND:

                          You might noticed that i used == when declaring a condition.Witho ut these it would submit message:

                          "Possibly incorrect assigment ........"

                          Savage

                          Comment

                          • Savage
                            Recognized Expert Top Contributor
                            • Feb 2007
                            • 1759

                            #14
                            Sorry for double post but i maked a mistake .The condition might become TRUE but I think that is not the best solution,anyway you would need to
                            "reset" a counter.


                            Savage

                            Comment

                            • brady
                              New Member
                              • Mar 2007
                              • 59

                              #15
                              Yea that makes sense because im not assigning values. Can i code it like this:
                              if(counter==num PerLine)
                              counter=counter %numPerLine;
                              cout <<"\n";
                              is this the same as how you had it on one line?

                              Originally posted by Savage
                              Here I can find 2 errors.

                              FIRST:


                              Code:
                              if(counter = counter%numPerLine)
                              is invalid.This condition will never become TRUE.

                              try using instead

                              Code:
                              if(counter==numPerLine) counter=counter%numPerLine,cout <<"\n";
                              SECOUND:

                              You might noticed that i used == when declaring a condition.Witho ut these it would submit message:

                              "Possibly incorrect assigment ........"

                              Savage

                              Comment

                              Working...