Link to home
Start Free TrialLog in
Avatar of mojeaux
mojeauxFlag for United States of America

asked on

Two dimensional array

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*/
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mojeaux

ASKER

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.