Pass array by reference

mojeaux
mojeaux used Ask the Experts™
on
Hi -
I'm stuck.    I'm having trouble incrementing an array that is being passed by reference.   When I run the code, I can get the output to generate 10 rows of either the first value  in the array or the last.. depending how I reference it.  However I'm not getting all the numbers in between.   I've made several attempts to increment the index, but no luck so far.   I need to speed things along.   Any suggestions?   Thanks!   Please see code below:

#include <stdio.h>  
#include <stdlib.h>  

/*GLOBAL DECLARATIONS*/

int main(void)
{
/*FUNCTION DECLARATIONS*/
int get_randno(void);

/*LOCAL DECLARATIONS*/

/*STATEMENTS*/

  get_randno();
   

  return(0);
}


/*~~~~~~~~~~~~~~~~~  get_randno Function ~~~~~~~~~~~~~~~
Generates random number
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

int get_randno(void)
{
/*FUNCTION DECLARATIONS*/
  void sigChk(int *pAry);
  void tableFmt(int *pTab, int count);

/* LOCAL DECLARATIONS */
  int count, i;
  char resp;
  int randmArray[100];

 
/* STATEMENTS */

for(count=0;count<100;count++)
      {
      i = rand();
        if ((i > 0) && (i <= 999))
        {
        randmArray [count] = i;
        sigChk(&i);
        printf ("Your array element is %d.  Your random number is %d\n", count, i);
        }
        else
        count--;
   }
  printf ("You now have 100 numbers in your array!\n\n");  
  system("pause\n\n");
  resp = 'N';
  printf ("Would you like to display your array in table format?\n");
  printf ("Please enter 'Y' for Yes and 'N' for No.\n\n");
  scanf ("%c", &resp);
      if (resp = 'Y')
      {
            tableFmt(randmArray, count);
          system("pause");
      }
      else if (resp = 'N')
      {
            system("pause");
            return (i);
      }
      else
      {
            printf("Please enter a valid response!!\n\n");
          system("pause");
      }
 
  return(i);
}/* counter loop*/


/*~~~~~~~~~~~~~~~~~  sigChk Function ~~~~~~~~~~~~~~~
Determines if random number evenly divisable by 3 or 7
and stores the random number as a negative number.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void sigChk(int *pAry)
{

/* LOCAL DECLARATIONS */
 
      
/* STATEMENTS */
if ((*pAry % 3 == 0) || (*pAry % 7 == 0))
      *pAry = -1 * *pAry;
else
    *pAry = 1 * *pAry;

return;
}/*changes sign on random number*/

/*~~~~~~~~~~~~~~~~~  tableFmt Function ~~~~~~~~~~~~~~~
Places random numbers in a 10 x 10 table.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void tableFmt(int *pTab, int count)
{

/* LOCAL DECLARATIONS */
# define TABLESIZE 100

int j;  
int tableCount;
      
/* STATEMENTS */
tableCount = 0;
for (j = 0; j < TABLESIZE; j++)
{
    printf("%5d", *pTab);
      if (tableCount < 9)
      {
            tableCount ++;
          count ++;
    }
      else
      {
            printf("\n");
            tableCount = 0;
      }

}
return;
}/*creates table of random numbers*/
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
mojeauxBusiness User

Author

Commented:
Sorry... should have stated it, but is the void tableFmt(int *pTab, int count)
 that is giving me problems.
CEO/Programmer
Commented:
printf("%5d", *pTab);

You always access the first Element of the Array.

What you want is pTab[j] to access an element somewhere in the array.
Of you prefer pointer accessing:  *(pTab + j)

I do not think you want a #define within a function.

You also should not place function prototyps within other functions. You may declare function pointers in a function but not the prototyps of functions.
You have several errors in your code besides the one fridom mentioned. For example, the statement

if ( resp = 'Y' )

will always succeed, because you are setting the variable resp to the value 'Y', then (implicitly) testing whether this value is equal to 0 (which it isn't, of course). What you want is

if ( 'Y' == resp ) ...

'=' is the assignment operator; '==' is the Boolean 'is equal to' relational operator. If you write it with the constant value first, the compiler will catch errors like this, and you'll save yourself HUNDREDS of hours of debugging time over your programming career.

As fridom says, you should not put a #define statement inside a function. Remember that #define statements (as with all preprocessor statements) are expanded before the C parser ever sees them, so they have no scope -- they're defined from the point you write them to the end of the source file containing them (unless you subsequently #undefine them).

There are various other problems with your program, but most of those are stylistic or have already been described in fridom's post.
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

mojeauxBusiness User

Author

Commented:
Sorry but I tried incrementing by 'j' as suggested however, it displays the first value and increments by one on the value, not the index.
evilrixSenior Software Engineer (Avast)

Commented:
Fridom has already answered the question for you (I think, since I reached the same conclusion); however, just a few things I thought I'd opine on...

>> You also should not place function prototyps within other functions.
There is actually nothing wrong with doing this (although it is rarely done). All it does is scope the declaration of the function to the scope it is declared in.

>>  if ((i > 0) && (i <= 999))
>>      {
>      randmArray [count] = i;

This is all largely unnecessary (the who if statement part of this). This should do what you want.

for(count=0;count<100;count++)
   randmArray [count] = rand() % 1000;

>> you should not put a #define statement inside a function
Well, other than style there is no technical reason that you shouldn't do this. I agree it is by no means best practice but other than create code that can be hard to debug it isn't, in itself, invalid code. That said, in this case I'd suggest you change this to a const C type rather than a defined value.

>> Sorry but I tried incrementing by 'j' as suggested
I suspect you've done this wrong. Can you show what you did? Since fridom has already identified this as the probabl issue I'll let him work with you on resolving this but it certainly won't hurt to show him what you've changed.
mojeauxBusiness User

Author

Commented:
Those other items were fixed as suggested I changed my code so that *(pTab +j) is working.   I had the pointer in the wrong location or the parenthises in the wrong place.    Thank you for the point in the right direction.   I have a couple more questions regarding the use of the pointer but will post a new question.   You all have been most helpful.   Thanks!
mojeauxBusiness User

Author

Commented:
Thank you for the assist.  A typo was preventing me from correctly incrementing the pointer.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial