mercredi 3 février 2021

How can I create a multidimensional array of random integers printed using a function?

Baby programmer here! I am stumped. I need help with an assignment and as concluded in my last question, my professor is no help, and I have done lots of research and I cannot find any examples or answers in my book or YouTube. In C, I have to use a loop to load random integer values into a 1 dimensional array and a multidimensional array, and use functions to print the contents of each array. I've taught myself how to load random integers into a 1 dimensional array, but I'm having trouble figuring out how to call it to a function to print the results. Similarly, I can create a multidimensional array and print the results through a function, but I can't figure out how to make the integers in a multidimensional array random. Here is what I've created and understand so far (It is a MESS and I thank you for patience in advance):

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

void printArray(int a[], size_t size); //prototype
void printArray(int b[2][3]); //prototype

int main(void)
{
    int n[10]; // n is an array of 10 integers
    int i;

    srand(time(NULL)); //uses time to make integers random

    printf("One-Dimensional Array\n");
    for (i = 0; i < 10; i++) //loop 10 times
    {
        n[i] = i + 1; 
        printf("%d ", (rand() % 50) + 1); //print random int from 1-100
    }

    printf("\nMulti-Dimensional Array\n");
    int array1[2][3] = { {1, 2, 3}, {4, 5, 6} };
    printArray(array1);
}

void printArray(int a[], size_t size)
{
    //insert function for printing 1d array
}
void printArray(int b[][3]) 
{
    for (size_t i = 0; i <= 1; ++i)
    {
        for (size_t j = 0; j <= 2; ++j)
        {
            printf("%d ", b[i][j]);
        }
        printf("\n");
    }
}



Aucun commentaire:

Enregistrer un commentaire