This is a modification of my previous version.
I would like advice on improving a FIFO algorithm for a 2D array that I've developed in ANSI C.
I have written code that uses this algorithm for test purposes. Random numbers are put into an array until all elements are full. The program then asks the user what numbers need to go in to the 2D array in order to update it. The code then removes the oldest array, shifts all remaining arrays up by one, and then adds the new array (as input by the user).
I am asking for a review of the FIFO code, fifo_algorithm
; the surrounding code is only for testing purposes. I would appreciate any suggestions that only use standard C.
code:
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
void random_num_input(void);
void print_random_num_input(void);
void print_new_array(void);
void fifo_algorithm(void);
int user_num[5] = { 0 };
uint16_t big_array[10][5] = { 0 };
uint16_t big_array_copy[10][5] = { 0 };
uint16_t array_print[1] = { 0 };
uint8_t count = 0;
int main()
{
random_num_input();
printf("These are the original numbers in the big array:\n");
print_random_num_input();
while (count < 10)
{
printf("Please enter values to swap in:\n");
for (int i = 0; i < 5; i++)
{
scanf("%d", &user_num[i]);
}
printf("\n\n");
fifo_algorithm();
count++;
}
}
//stores random numbers between 0-255 into big_array and big_array_copy
void random_num_input(void)
{
for (int j = 0; j < 10; j++)
{
for (int i = 0; i < 5; i++)
{
big_array[j][i] = rand() % 255;
}
}
}
//prints the random values within the big_array
void print_random_num_input(void)
{
for (int j = 0; j < 10; j++)
{
for (int i = 0; i < 5; i++)
{
array_print[0] = big_array[j][i];
printf("%d,", array_print[0]);
}
printf("\n");
}
}
//This algorithm removes the oldest array in big_array_copy (element 9), shifts every
//element up by 1 and stores the user inputted array into element 0. Thus a fifo that updates
//the 2d array with the user input values every time
void fifo_algorithm(void)
{
for (int j = 0; j < 10; j++)
{
for (int i = 0; i < 5; i++)
{
big_array_copy[j][i] = big_array[j][i];
}
}
for (int k = 0; k < 5; k++)
{
big_array_copy[10 - 1][k] = 0;
}
for (int j = 9; j > 0; j--)
{
for (int i = 0; i < 5; i++)
{
big_array_copy[j][i] = big_array[j - 1][i];
}
}
for (int i = 0; i < 5; i++)
{
big_array_copy[0][i] = user_num[i];
}
print_new_array();
for (int j = 0; j < 10; j++)
{
for (int i = 0; i < 5; i++)
{
big_array[j][i] = big_array_copy[j][i];
}
}
}
//simply prints the new, updated values in big_array_copy
void print_new_array(void)
{
printf("These array values have been swapped:\n ");
for (int j = 0; j < 10; j++)
{
for (int i = 0; i < 5; i++)
{
array_print[0] = big_array_copy[j][i];
printf("%d,", array_print[0]);
}
printf("\n");
}
}
Thanks