school assignment annoyance

From: Paul Fedorenko (pfedorenko_at_look.ca)
Date: 07/30/04


Date: Thu, 29 Jul 2004 23:17:15 -0400

We just learned to work with arrays today. Fun stuff, that. Anyway,
I'm working on this little program that reads a bunch of random
numbers into a 2-dim array, displays them and gives the user the
option of searching the array for particular numbers.

when it finds a number, the program is supposed to tell the user that
the number was found, and where. If the specified number is not found
anywhere in the array, it's supposed to display a single message to
that effect.

Anyway, everything works. Sort of. I've got a function that
basically contains a for loop that searches teh array for numbers and
prints the results. But instead of displaying the "number not found"
message once at the end, it displays it for every iteration of the
loop that comes up negative. I realise that I should probably put the
message outside the loop, I'm just not sure what, if anything, to put
in its place. Right now, I'm using a for loop, as you can see. Would
a while loop be better?

Here's the code in question:

void search(num)
{
        for (suba = 0; suba < 4; suba++)
        {
                for (subb = 0; subb < 8; subb++)
                {
                        if (array[suba][subb] == num)
                        {
                                printf("%d is in the array at position
%d, %d.\n", num, suba+1, subb+1);
                        }
                        else
                        {
                                printf("%d was not found.\n", num);
                        }
                }
        }
        fflush(stdin);
        getchar();
}

Oh... suba and subb are subscripts used for displaying/finding
specific elements within the array, as in array[suba][subb]. array is
obviously the name of my array, and num is the number being searched
for, passed from another function.

The getchar(); at the end is there to pause the output so I can see
what's going on. It'll be replaced with something a little friendlier
looking in the final version.

Any wraping is the result of my newsreader, not my coding...

Any suggestions would be much appreciated. The rest of the program
works, except for this rather inelegant bit.



Relevant Pages

  • Re: Trying to build a page that will read a csv file...
    ... > I can't figure out how to get the results to display in a multi-column ... >> Use fileto load the csv file into an array. ... >> Use a loop to iterate through every element in the array, ...
    (php.general)
  • Re: pass an array throuh a function
    ... >The array would record the data from the user from one function.AND another ... >once the user is done and send it to the display function. ... your displayfunction is going through a loop. ... int count; ...
    (alt.comp.lang.learn.c-cpp)
  • RE: Error 3021
    ... Create proto-file names using the selected job names and storre to an array ... Save and close the document and repeat the loop ... Dim strJobsAs String, strDocsAs String, varValsAs _ ...
    (microsoft.public.access.modulesdaovba)
  • RE: Error 3021
    ... Kevin Backmann ... Create proto-file names using the selected job names and storre to an array ... Save and close the document and repeat the loop ... Dim strJobsAs String, strDocsAs String, varValsAs _ ...
    (microsoft.public.access.modulesdaovba)
  • RE: Error 3021
    ... Create proto-file names using the selected job names and storre to an array ... Save and close the document and repeat the loop ... Dim strJobsAs String, strDocsAs String, varValsAs _ ...
    (microsoft.public.access.modulesdaovba)