Two dimensional array

mojeaux
mojeaux used Ask the Experts™
on
Hi -
I'm attempting to create a two dimenional array from an existing one dimensional array. (see function create_2dim).  I'm not getting any compiler errors, but when the code runs, I'm getting addresses rather than values from the one dimensional array (randmArray).  I think my assignment in the create_2dim is incorrect.   Please see code below and advise.  Many Thanks!  M

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

/*GLOBAL DECLARATIONS*/

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

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

/*STATEMENTS*/
 
 get_randno(randmArray, 100);
 create_2dim(randmArray, 100);
 
  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;
  int count;
  int randmArray[100];
 
/* STATEMENTS */

printf("STEP 1:  Create one dimenaional Array populated with random numbers and then manipulate those numbers.\n\n");
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, 100);
      }
      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*/

/*~~~~~~~~~~~~~~~~~ create_2dim Function ~~~~~~~~~~~~~~~
Create two dimensional array from one dimensional array
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void create_2dim(int*prandAry, int cnt)
{
/*FUNCTION DECLARATIONS*/

/* LOCAL DECLARATIONS */
      int randmArray[100];
      int twoDim[10][10];
      int r, c;
 
/* STATEMENTS */
printf("\n\nSTEP 2:  Convert one dimensional Array to a two dimensional Array.\n\n");
printf("Your new TWO Dimensional Array is displayed in the matrix below:\n\n");
      for(r=0; r<10; r++)
      {
            for(c=0; c<10; c++)
            {
            twoDim[r][c] = *(prandAry+1);

            printf("\t %d",twoDim[r][c]);  
            }
         printf("\n");
      }
      system("pause");

    return;
}/* create two dimensional array*/
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2009
Commented:
Your get_randno function never places anything in the prandAry array. Instead, it fills the local array randmArray.

And in the create_2dim function :

>>             twoDim[r][c] = *(prandAry+1);

You're never modifying prandAry, so this will assign the same value over and over again to all items in the 2D array.
ozo
Most Valuable Expert 2014
Top Expert 2015
Commented:
void get_randno(int *prandAry, int *cnt) should be
void get_randno(int *prandAry, int cnt)
(or get_randno(randmArray, 100); should be something different)

but since  *prandAry is never used in get_randno
  int randmArray[100];
is  never initialized and  create_2dim(randmArray, 100) is passed a pointer to uninitialized memory
Top Expert 2016
Commented:
if you would apply the recommended changes in http://www.experts-exchange.com/Programming/Languages/Q_27676706.html#a37854510, most of the current problems would be solved either.

Sara
mojeauxBusiness User

Author

Commented:
Infinity08 & Ozo:  Thank you for the explaining 'not referencing' prandAry.   Once I made those changes it worked perfectly.

Sara:  I went back and referenced your link to the use of the sizeof function.   However, I dont have any knowledge of this didnt feel comfortable using it yet.... at least until I am more comfortable with pointers and arrays.   That aside, you have been a tremendous help.   Thanks so much.

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