0% found this document useful (0 votes)
3 views6 pages

Random Number Table Operations

gg

Uploaded by

Marouane Al Asri
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Random Number Table Operations

gg

Uploaded by

Marouane Al Asri
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <stdio.

h>

Int main() {

// Seed the random number generator

Srand(time(0));

// Step 1: Generate a table of random numbers between 0 and 9

Int originalTable[8][8];

Printf(“Step 1: Generated Table\n”);

For (int I = 0; I < 8; i++) {

For (int j = 0; j < 8; j++) {

originalTable[i][j] = rand() % 10;

printf(“%d “, originalTable[i][j]);

Printf(“\n”);

// Step 2: Find the row sums in the table

Printf(“\nStep 2: Row Sums\n”);

For (int I = 0; I < 8; i++) {

Int rowSum = 0;

For (int j = 0; j < 8; j++) {

rowSum += originalTable[i][j];

Printf(“Row %d Sum: %d\n”, I, rowSum);

// Step 3: Find the column sums in the table


Printf(“\nStep 3: Column Sums\n”);

For (int j = 0; j < 8; j++) {

Int columnSum = 0;

For (int I = 0; I < 8; i++) {

columnSum += originalTable[i][j];

Printf(“Column %d Sum: %d\n”, j, columnSum);

// Step 4: Create a new table based on specific conditions

Int newTable[8][8];

Printf(“\nStep 4: New Table\n”);

For (int I = 0; I < 8; i++) {

For (int j = 0; j < 8; j++) {

If (originalTable[i][j] > 4) {

newTable[i][j] = 9;

} else {

newTable[i][j] = 0;

Printf(“%d “, newTable[i][j]);

Printf(“\n”);

// Step 5: Find the count of every digit in the table and print them

Int digitCount[10] = {0}; // Array to store the count of each digit (0-9)

For (int I = 0; I < 8; i++) {


For (int j = 0; j < 8; j++) {

digitCount[originalTable[i][j]]++;

Printf(“\nStep 5: Count of each digit in the table\n”);

For (int digit = 0; digit < 10; digit++) {

Printf(“Digit %d Count: %d\n”, digit, digitCount[digit]);

// Step 6: Show the count of every digit in a tabular form

Printf(“\nStep 6: Count of each digit in a tabular form\n”);

Printf(“Count\t0\t1\t2\t3\t4\t5\t6\t7\t8\t9\n”);

For (int I = 9; I >= 0; i--) {

Printf(“%d\t”, I + 1);

For (int digit = 0; digit < 10; digit++) {

If (digitCount[digit] > i) {

Printf(“x\t”);

} else {

Printf(“ \t”);

Printf(“\n”);

Printf(“\t1\t2\t3\t4\t5\t6\t7\t8\t9\n”);

// Step 7: Counts of two consecutive digits

Printf(“\nStep 7: Counts of two consecutive digits\n”);


For (int I = 17; I >= 10; i--) {

Printf(“%d\t”, i);

For (int digit = 0; digit < 10; digit += 2) {

If (digitCount[digit] + digitCount[digit + 1] >= i) {

Printf(“x\t”);

} else {

Printf(“ \t”);

Printf(“\n”);

Printf(“\t01\t23\t45\t67\t89\n”);

// Step 8: Find and print the flow from top left to bottom right

Printf(“\nStep 8: The flow from top left to bottom right\n”);

For (int I = 0; I < 8; i++) {

For (int j = 0; j < 8; j++) {

Printf(“%d\t”, originalTable[i][j]);

Printf(“\n”);

Printf(“\n”);

// Step 9: Read a 2x2 grid from the keyboard and find the most similar grid

Printf(“Step 9: A 2×2 grid entered and closest 2×2 grid from table\n”);

Int userInputGrid[2][2];
For (int I = 0; I < 2; i++) {

For (int j = 0; j < 2; j++) {

Scanf(“%d”, &userInputGrid[i][j]);

Int minDifference = -1;

Int mostSimilarGrid[2][2];

For (int I = 0; I < 7; i++) {

For (int j = 0; j < 7; j++) {

Int currentDifference = 0;

For (int x = 0; x < 2; x++) {

For (int y = 0; y < 2; y++) {

currentDifference += abs(userInputGrid[x][y] – originalTable[I + x][j + y]);

If (minDifference == -1 || currentDifference < minDifference) {

minDifference = currentDifference;

for (int x = 0; x < 2; x++) {

for (int y = 0; y < 2; y++) {

mostSimilarGrid[x][y] = originalTable[I + x][j + y];

}
}

Printf(“\nmd_grid:\n”);

For (int I = 0; I < 2; i++) {

For (int j = 0; j < 2; j++) {

Printf(“%d “, mostSimilarGrid[i][j]);

Printf(“\n”);

Return 0;

You might also like