Pointers and Arrays

mojeaux
mojeaux used Ask the Experts™
on
Hi - As you can tell by my posts, I'm in the learning stages of the C programs.  I think I goofed by not declaringin my array in the main, rather than the get_randno() function.   I need to pass the array values back to the main so I can use them to generate a two dimensional array in the next function.   Do I need to completely re-write get_randno()?

Thanks!   M

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

/*GLOBAL DECLARATIONS*/

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

/*LOCAL DECLARATIONS*/

/*STATEMENTS*/

 get_randno();
   

  return;
}

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

void get_randno(void)
{
/*FUNCTION DECLARATIONS*/
  void tableFmt(int *pTab, int count);
  void sigChk(int *pAry);
 
/* LOCAL DECLARATIONS */
  int count;
  int randmArray[100];
  char resp1;
 
/* STATEMENTS */

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

      printf ("Your array element is %d.  Your random number is %d\n", count, randmArray[count]);
   }

      printf("You now have 100 random numbers in your array!\n\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", &resp1);
      if (resp1 == 'Y')
      {
            tableFmt(randmArray, count);
            system("pause");
      }
      else if (resp1 == 'N')
      {
            printf("Sorry you do not wish to continue.  Program will end now.  Thank you.\n\n");
            system("pause");
            return;
      }
      else
      {
            printf("Invalid response - unable to continue with table format.  Thank you.\n");
            system("pause");
            return;
    }  
    return;
}/* 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)
{
/*FUNCTION DECLARATIONS*/

/*int sumNo(int sum);*/
 
/* LOCAL DECLARATIONS */
int j, rvalue, evenCt, sumTot;  
int tableCount;
int tableSize;
      
/* STATEMENTS */
tableCount = 0;
tableSize = 100;
evenCt = 0;
sumTot = 0;
for (j = 0; j < tableSize; j++)
{
    printf("%5d", *(pTab+j));
      rvalue = *(pTab+j);
      if (rvalue % 2 == 0)
            evenCt = evenCt +1;
      sumTot = sumTot + rvalue;
      if (tableCount < 9)
      {
            tableCount ++;
      }
      else
      {
            printf("\n");
            tableCount = 0;
      }
      
}
printf("\nThe total number of EVEN numbers in this array is:  %d\n\n", evenCt);
printf("\nThe SUM TOTAL of all the numbers in this array is:  %d\n\n", sumTot);
system("pause");


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

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Doesn't need to be re-written; just need to move the declare for randmArray[100] up to the main function...  Also do this for count if you will need it later as well...

You need to learn about "variable persistence"...

Variables declared at the main level are "global" to the source unit and may be referenced without need to pass to subroutines...

Variables declared within a subroutine are "automatic" and will "disappear" after the function returns!  Thus, the way it is currently coded, that array is freed after the subroutine that declares it returns and can no longer be referenced at the main level or passed to subsequent routines.
Top Expert 2016

Commented:
if you follow the advice n2fc has given, the get_randno would get same argument (type) as function sigChk and additional the array size (max number of elements) as int argument.

Sara
mojeauxBusiness User

Author

Commented:
Excellent.    Thanks for the advice.   As soon as I get home from work, I will jump on this tonight and get it going in the right direction again.   I will search google for variable persistence.   You guys are awesome!
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

mojeauxBusiness User

Author

Commented:
Ok ... I moved the variables and array and it completely jacked up the other functions in the program.   =(    My compiler doesnt like the new change.   I'm getting:
warning C4047: 'function' : 'int' differs in levels of indirection from 'int *'
warning C4024: 'get_randno' : different types for formal and actual parameter 2
warning C4028: formal parameter 2 different from declaration
error C2065: 'count' : undeclared identifier
error C2065: 'count' : undeclared identifier
error C2065: 'count' : undeclared identifier
error C2065: 'randmArray' : undeclared identifier
error C2065: 'count' : undeclared identifier
error C2109: subscript requires array or pointer type
error C2065: 'randmArray' : undeclared identifier
error C2065: 'count' : undeclared identifier
error C2109: subscript requires array or pointer type
error C2198: 'sigChk' : too few arguments for call
error C2065: 'count' : undeclared identifier
error C2065: 'randmArray' : undeclared identifier
error C2109: subscript requires array or pointer type
error C2065: 'randmArray' : undeclared identifier
error C2065: 'count' : undeclared identifier
error C2100: illegal indirection

I think it just went from bad to worse.... This pointer concept and arrays is killing me.  Sigh!

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

/*GLOBAL DECLARATIONS*/

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

/*LOCAL DECLARATIONS*/
  int randmArray[100];
  int count;

/*STATEMENTS*/
 count = 0;
 get_randno(&randmArray[100], &count);
   

  return;
}

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

void get_randno(int *prandAry, int *cnt)
{
/*FUNCTION DECLARATIONS*/
  void tableFmt(int *pTab, int count);
  void sigChk(int *pAry);
 
/* LOCAL DECLARATIONS */
  char resp1;
 
/* STATEMENTS */

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

      printf ("Your array element is %d.  Your random number is %d\n", count, randmArray[count]);
   }

      printf("You now have 100 random numbers in your array!\n\n");
      printf("Would you like to display your array in table format (Y/N)?\n");
    scanf_s("%c", &resp1);
      if (resp1 == 'Y')
      {
            tableFmt(&randmArray, *count);
            system("pause");
      }
      else if (resp1 == 'N')
      {
            printf("Sorry you do not wish to continue.  Program will end now.  Thank you.\n\n");
            system("pause");
            return;
      }
      else
      {
            printf("Invalid response - unable to continue with table format.  Thank you.\n");
            system("pause");
            return;
    }  
    return;
}/* 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)
{
/*FUNCTION DECLARATIONS*/

/*int sumNo(int sum);*/
 
/* LOCAL DECLARATIONS */
int j, rvalue, evenCt, sumTot;  
int tableCount;
int tableSize;
      
/* STATEMENTS */
tableCount = 0;
tableSize = 100;
evenCt = 0;
sumTot = 0;
for (j = 0; j < tableSize; j++)
{
    printf("%5d", *(pTab+j));
      rvalue = *(pTab+j);
      if (rvalue % 2 == 0)
            evenCt = evenCt +1;
      sumTot = sumTot + rvalue;
      if (tableCount < 9)
      {
            tableCount ++;
      }
      else
      {
            printf("\n");
            tableCount = 0;
      }
      
}
printf("\nThe total number of EVEN numbers in this array is:  %d\n\n", evenCt);
printf("\nThe SUM TOTAL of all the numbers in this array is:  %d\n\n", sumTot);
system("pause");


return;
}/*creates table of random numbers*/
Top Expert 2016
Commented:
if you would rename the passed 'prandAry' to 'randmArray' most of the errors regarding 'randmArray'  would be solved.

then change the second argument of get_randno from int* to int and name the argument variable maxcount.

you then would call the function in main

get_randno(randmArray, 100);

Open in new window


or - better -

get_randno(randmArray, sizeof(randmArray)/sizeof(randmArray[0]));

Open in new window


the second uses the sizeof function to compute the count of elements from total size diveded by size of a single element.

note the sizeof for arrays only works in the function where the array was defined. if you passed the array to a function the variable turned to a pointer which could be used like an array but only has pointer size.

then in  the for statement in get_randno you change the < 100  to < maxcount.

post the remaining errors after you made the changes.

Sara
Top Expert 2016

Commented:
i would recommend that for every change you do, you should try to find out why that change is necessary. if you don't find an answer it is a good chance that the change was made wrong. for example an int* is a pointer to int and also can point to the first element of an array. so, in a function you could use a pointer like an array (of course only if you know for sure it is an array). but as told if you only have a pointer you don't know the number of elements in the function. that is why it is important to always pass the number of elements together with an array and this argument should be an integer and not the pointer to an integer.

Sara
mojeauxBusiness User

Author

Commented:
Thanks Sara!  I'm back on track and everything as it did before.  Great explanation!

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