0

Write a declaration for a two dimensional array A that has 5 rows and 10 columns and in which each element is a character string of length 20. What is the sizeof A?

Part II: Write statements to set each element in the array to a string of blanks.

This is first year programming so everything is very basic. This is in C.

Thanks for any help.

Here's my attempt for part I

char c[5][10][20];

Here's my attempt for part II

int x,y;  
for (x=0;x<5;x++) {
     for (y=0;y<10;y++) {
         c[x][y]=" "; }}
10
  • 2
    Hello and welcome to StackOverflow! Have you tried anything yet? It might be helpful to post what you've attempted so far. Commented Dec 19, 2015 at 2:12
  • 2
    Welcome to Stack Overflow. Please read the About page soon. Generally speaking, we'll help people resolve problems with their code, but we don't go writing the code from scratch. What have you tried? What is causing you problems? The problem is only mildly confusing in that you probably end up with a 3D array to satisfy the requirements of the problem. How many different ways could you set the strings to blanks? There are at least 3 options, probably far more. Are you allowed to use standard library functions? Commented Dec 19, 2015 at 2:22
  • @JiaJian updated my question with my attempt Commented Dec 19, 2015 at 2:25
  • @JonathanLeffler I believe I can use the standard library functions, I also updated with my attempts. This professor is very confusing with his wording so I try my best. Commented Dec 19, 2015 at 2:27
  • Part I looks correct to me. How big is that array? How did you calculate that size? How would you check that computation with a printing operation in your code? Part II has problems — major problems. You can't assign strings like that. The outer two loops are OK (but the Pico-style layout is excruciating to observe in C code — please don't use it; use Allman or 1TBS). Commented Dec 19, 2015 at 2:31

3 Answers 3

1

I don't know if I'm misreading your question or whether it really is as straight forward as finding the size of your 5 * 10 * 20 character array? The sizeof type char is 1-byte per-char. Thus 5 * 10 * 20 = 1000 (as compared to say a type int which would normally be 4-bytes per-int).

To determine the size within the scope where the array is statically declared you can use the sizeof operator, for example:

#include <stdio.h>

int main (void) {

    char c[5][10][20];

    printf ("\n sizeof c : %lu\n\n", sizeof c);

    return 0;
}

Use/Output

$ ./bin/sizeof_3d

 sizeof c : 1000

To set each element in the array to ' ' (space), you have a couple of options (1) a loop over each element in the array; or (2) using memset. To use a loop:

size_t i, j, k;
...
for (i = 0; i < 5; i++)
    for (j = 0; j < 10; j++)
        for (k = 0; k < 20; k++)
            c[i][j][k] = ' ';

If you need a literal string of blanks you will need to add a nul-terminating character as the final character in each string. When using the loop above, that can be accomplished by replacing c[i][j][k] = ' '; with the following that uses a ternary operator to write a nul-character (zero) as the final character in each string:

            c[i][j][k] = k + 1 == 20 ? 0 : ' ';

To use memset (don't forget to #include <string.h>):

memset (c, ' ', sizeof c);

To null terminate following the use of memset, an abbreviated loop can be used:

size_t i, j, k;
...
memset (c, ' ', sizeof c);
for (i = 0; i < 5; i++)
    for (j = 0; j < 10; j++)
        c[i][j][19] = 0;

Let me know if you have further questions.

Sign up to request clarification or add additional context in comments.

2 Comments

Null termination? It's not entirely clear whether that's actually required or not, but it usually is.
Agreed - According to the question statement he was to fill the array with a string of blanks. Technically, that would require nul-termination, but it is somewhat unclear.
0

First declare a type for your 20 character strings.

typedef struct mystring {
    char str[20];
} mystring;

Then you can declare your variable A LIKE THIS:

mystring A[5][10];

So, A is a variable of type 5 string which has 5 rows and 10 columns. Each element in the array is of type mystring which as a string that can hold 20 characters. The maximum length of a string that a mystring can hold is of course 19 characters since you need to reserve one character for the null terminator.

Comments

0

An array A that has 5 rows and 10 columns and in which each element is a character string of length 20.

  1. What is the sizeof A?

The size of array A we get by multiplying its columns by its rows and the result of it multiplying by the size of the elements of the array. In this case they are strings of 20 characters length. Assuming your machine's char implementation is 1 byte, then array A occupies total size of:

5 x 10 x 20 = 1000 bytes or 1MB.

  1. To define such an array in C++, you write within your main, avoiding magic numbers:
 size_t rows_number = 5;
 size_t columns_number = 10;
 size_t element_size = 20;

 char c[rows_number][columns_number][element_size];
  1. To initialize its elements to particular value, you could use for loops for each index of the array:
char initial_value = ' ';

for (i = 0; i < row_number; i++) {
    for (j = 0; j < column_number; j++) {
        for (k = 0; k < element_size; k++) {
            c[i][j][k] = initial_value;
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.