0% found this document useful (0 votes)
43 views3 pages

Got It

Uploaded by

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

Got It

Uploaded by

sazidalmubin11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Got it!

Let's go through each question and answer:

1. Using sizeof(a) / sizeof(a[0]) vs sizeof(a) / sizeof(t):


o sizeof(a) / sizeof(a[0]) is preferred because it directly calculates the
number of elements in the array a by dividing the total size of the array by the
size of its first element. This method is more robust and less error-prone because
it doesn't require you to explicitly specify the type of the array's elements.
o sizeof(a) / sizeof(t) is considered inferior because it requires you to
manually specify the type t of the array's elements. If the type of the elements
changes, you would need to update the code in multiple places, increasing the risk
of errors.
2. Using a digit (in character form) as a subscript:
o To use a digit in character form as an array subscript, you can convert the
character to its corresponding integer value. For example, if you have a character
char digit = '3';, you can use digit - '0' to get the integer value 3, which
can then be used as an array subscript.
3. Declaration of an array named weekend containing seven bool values:

bool weekend[7] = {true, false, false, false, false, false, true};

4. Using a designated initializer for the weekend array:

bool weekend[7] = {[0] = true, [6] = true};

5. Filling an array with the first 40 Fibonacci numbers:

int fib_numbers[40];
fib_numbers[0] = 0;
fib_numbers[1] = 1;
for (int i = 2; i < 40; i++) {
fib_numbers[i] = fib_numbers[i - 1] + fib_numbers[i - 2];
}

6. Filling the segments array for seven-segment displays:

const int segments[10][7] = {


{1, 1, 1, 1, 1, 1, 0}, // 0
{0, 1, 1, 0, 0, 0, 0}, // 1
{1, 1, 0, 1, 1, 0, 1}, // 2
{1, 1, 1, 1, 0, 0, 1}, // 3
{0, 1, 1, 0, 0, 1, 1}, // 4
{1, 0, 1, 1, 0, 1, 1}, // 5
{1, 0, 1, 1, 1, 1, 1}, // 6
{1, 1, 1, 0, 0, 0, 0}, // 7
{1, 1, 1, 1, 1, 1, 1}, // 8
{1, 1, 1, 1, 0, 1, 1} // 9
};

7. Shrinking the initializer for the segments array:

const int segments[10][7] = {


{1, 1, 1, 1, 1, 1, 0}, // 0
{0, 1, 1}, // 1
{1, 1, 0, 1, 1, 0, 1}, // 2
{1, 1, 1, 1, 0, 0, 1}, // 3
{0, 1, 1, 0, 0, 1, 1}, // 4
{1, 0, 1, 1, 0, 1, 1}, // 5
{1, 0, 1, 1, 1, 1, 1}, // 6
{1, 1, 1}, // 7
{1, 1, 1, 1, 1, 1, 1}, // 8
{1, 1, 1, 1, 0, 1, 1} // 9
};

8. Declaration for a two-dimensional array named temperature_readings:

float temperature_readings[30][24];

9. Computing the average temperature for a month:

float sum = 0.0;


int count = 0;
for (int day = 0; day < 30; day++) {
for (int hour = 0; hour < 24; hour++) {
sum += temperature_readings[day][hour];
count++;
}
}
float average_temperature = sum / count;

10. Declaration for an 8 × 8 char array named chess_board:

char chess_board[8][8] = {
{'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'},
{'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
{'.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.'},
{'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'},
{'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'}
};

11. Using a loop to store data into an 8 × 8 char array named checker_board:

char checker_board[8][8];
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if ((i + j) % 2 == 0) {
checker_board[i][j] = 'B';
} else {
checker_board[i][j] = 'R';
}
}
}

I hope this format is more helpful! If you have any more questions or need further clarification,
feel free to ask.24681012

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy