samedi 24 février 2018

Calculations using random numbers of a specific range in a 2D array in c

I have to populate a 2D array using random numbers between 3 and 19. The array is 4 x 3, the first two columns will represent two sides to a right triangle and the third column is the hypotenuse. I'm pretty new to c, so I'm not sure where I'm going wrong. my output gives me the headers just fine but just one single vertical line of numbers instead of a 4 x 3 grid. Here's my code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define ROW 4
#define COL 3

void printChart(double array[ROW][COL]);

int main(void)
{
    double chart[ROW][COL];
    double *ptrchart = &chart[0][0];

    srand(time(NULL));
    for (size_t i = 0;i < ROW;i++)
    {
        for (size_t j = 0;j < COL;j++)
        {

            chart[i][j] = 3 + (rand() % 19);
        }
    }

    for (size_t i = 0;i < ROW;i++)
    {
        chart[i][2] = 0;
    }

    printChart(ptrchart);

    for (size_t i = 0;i < ROW;i++)
    {
        chart[i][2] = (double)sqrt(pow(chart[i][0], 2) + pow(chart[i][1], 2));

    }

    puts(" ");

    printChart(ptrchart);

    return 0;

}

void printChart(double array[ROW][COL])
{
    printf("%s", "Side A\tSide B\tHypotenuse(Side C)\n");
    for (size_t i = 0;i < ROW;i++)
    {
        for (size_t j = 0;j < COL;j++)
        {
            printf("%.3f\t",  array[i][j]);

            if (j = 2)
            {
                puts(" ");
            }
        }
    }
}

Any help would be greatly appreciated, and if I need to clarify anything please let me know.




Aucun commentaire:

Enregistrer un commentaire