0

I am trying to answer a question in my C programming book, but I am not sure if my answer is correct.
The book doesn't provide any answers though. I am new to C programming, and any help will be appreciated.
Question:
Assume you have declared an array as follows:

float data[1000];

Show two ways to initialize all elements of the array to 0.
Use a loop and an assignment statement for one method, and the memset () function for the other.

my current code:

#include <stdio.h>

float data[1000]; 

main(){
    int count;
    for (count = 0; count < 1000; count++){
        scanf("%f", &data[count]);
    }
    for (count = 0; count < 1000; count++){
        printf("Array %i: %f\n", count, data[count]);
    }
}
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
tomcruzana
  • 39
  • 1
  • 6

5 Answers5

2

A few points to help you get started:

  • You're trying to set all items to 0. Scanf requires you to input all values. This isn't necessary as you could just set them to 0 with data[count]=0.0f; inside of that for loop.
  • memset is a function that will do something similar for you (including the for loop). Have a look at the documentation of memset:

memset

void * memset ( void * ptr, int value, size_t num );

Fill block of memory Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char).

Parameters

  • ptr: Pointer to the block of memory to fill.
  • value: Value to be set. The value is passed as an int, but the function fills the block of memory using the unsigned char conversion of this value.
  • num: Number of bytes to be set to the value. size_t is an unsigned integral type.

You should notice that memset only works with bytes. So you can use it to set a float to 0, as it consists of 4 bytes that are all 0, but you cannot set it to 1 for instance. But as notices by other users, this just happens to be so on most hardware.

Lanting
  • 3,060
  • 12
  • 28
  • Also notice (it is not a solution but a fact) just by having `data` defined as an `external` (i.e global) variable in C it is already initialized with its bytes in 0 as if you had used `memset()`. – Fernando Jan 08 '15 at 16:23
0
memset(data,0,sizeof(data));

can be used to assign all the elements of the array to 0

Gopi
  • 19,784
  • 4
  • 24
  • 36
  • 2
    There seems to be somebody who is downvoting all answers, just don't pay attention. – Jens Gustedt Jan 08 '15 at 10:30
  • @JensGustedt do stackexchange has any mechanisim to detect such downvoters ? – Jeegar Patel Jan 08 '15 at 10:35
  • Note that `sizeof` is not a function, so that can be written as just `memset(data, 0, sizeof data);`. – unwind Jan 08 '15 at 10:41
  • 3
    @unwind Even the reference manual says it can be used in either of ways `sizeof(data)` is totally fine IMO http://en.cppreference.com/w/cpp/language/sizeof – Gopi Jan 08 '15 at 10:44
  • @Gopi Why are you quoting a C++ document in a C question? And of course it works, but it works just because the argument is an expression (the expression `data`) that is put inside parentheses, which of course is also an expression, i.e. `(data)`. People write it like a function call though, which is confusing and which is something I tend to point out. – unwind Jan 08 '15 at 10:48
  • @unwind Just wanted to show that the `sizeof operator` can be used in both the syntax. – Gopi Jan 08 '15 at 10:51
  • @Mr.32, yes, there is such a detection. But it can't be perfect. – Jens Gustedt Jan 08 '15 at 10:52
  • @unwind You can either use `sizeof(any)` *or* `sizeof(any)`,`sizeof var`. The former variant is more consistent and readable. – 2501 Jan 08 '15 at 10:59
  • @2501 I think I didn't understand what you were trying to say, sorry. – unwind Jan 08 '15 at 11:06
  • @unwind No problem. I suggest you look up topics on readability and consistency. – 2501 Jan 08 '15 at 11:10
  • @2501 Why did you introduce two new names (`any` and `var`), and what do you mean by the part after the "*or*"? I didn't fail to understand the concepts of readability and consistency, I failed to understand the "variants" you were talking about. – unwind Jan 08 '15 at 11:11
  • @unwind Two different styles separated by *or*. Try coding examples with both styles and you will see the difference. – 2501 Jan 08 '15 at 11:12
-1

Show two ways to initialize all elements of the array to 0. Use a loop and an assignment statement for one method, and the memset () function for the other.

1) Using for loop and assignment statement

for(i = 0; i<1000; i++)
    data[i] = 0.0f;

2) Using memset function

memset(&data,0,sizeof(data));

Short and simple way supported by c99 spec

float data[1000] = {0.0f};

declare array like this.

Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
-1

Actually there are at least three methods.:)

// first
float data[1000] = { 0.0f };

// second
size_t i;
for ( i = 0; i < 1000; i++ ) data[i] = 0.0f;

// third
memset( data, '\0', sizeof( data ) );

You can also add one more method in C99

// forth
float data[1000] = { [0] = 0.0f };

In C++ you could use also the following declaration

float data[1000] = {};

or

float data[1000] {};
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
-2

I don't think there is a need for scanf("%f",&data[count]); instead you can directly assign it with data[count]=0;

and if you use the memset() function you can set value to zero directly to an array memset(data,'0',1000);

Alex
  • 5,240
  • 1
  • 31
  • 38