# C++ set all array values to 0

1 vote
216 views

## Problem:

Hello good people,

I started learning C++ programming for a few days. I just come to the arrays chapter. Well, I can see a lot of variation in a declaration of Arrays. I want to have an array of the same values in it. Let’s say, an array that has five values in it and all are zero.

1 vote

## Solution:

There are different ways to initialize an array in C++. An array is a collection of data that holds a fixed number of values of same type. For example: if you want to store a salary of 100 employees, you can create an array for it.

``int salary[100];``

As you are asking for an array full of zero, you want to write this way

``int number[5] = {0, 0, 0, 0, 0};``

or

``int num[5] =  { 0 };``

Above these are the naive method to initializing an array. As your question headline asked for set all array values to 0. You may want to write a program that sets all the value to zero. if so, the solution is the below program:

``````#include<stdio.h>
int main(void){
int size = 5;
int value = 0;
int arr[size];
int i;
for (i = 0; i < size ; i++){
arr[i] = value;
}
printf("The array is:");
for (i = 0; i < size ; i++){
printf("%d ", arr[i]);
}
return 0;
}``````

You are all set to go now. Good Day!

## Related questions

3 views
Problem I want to initialize an array in a same value, how can i do it? Anyone suggest me.
5 views
Problem: Hi there! I am a new programmer and while playing with the arrays, I wrote the following code: #include<iostream> using namespace std; int main() { int arr[100] = {0}; for(int i=0; i<100; i++) { cout<<arr[i]; } } When I run this code, it ... the output. I want to know that why this program is printing the 0 for 100 times whereas I have assigned 0 to the array only once. Please explain!
2 views
Problem: When an array of objects is declared but not initialized, how can I tell if the array values are null?
13 views
Problem: In one task I am required to implement a function of the form B** f(string, int&);(unmodifiable prototype) where the return value (as I understand it) is a pointer to an array of type pointersB . The case is that I want to generate, from a static arrangement of elements of a type A, a static ... *b; string s=""; int n=1; a.set('A'); b=x.f(s,n); cout<<b[0]->get()<<endl; return 0; }
21 views
#include <iostream> #include <math.h> #include <vector> using namespace std; int main () {      int N, A;      int Ratings [N] [A];      int cont5, cont4, cont3, cont2, cont;      int notes [cont3];      cout << "Enter number of students." << endl;      cin >> A;      cout << " ... 2d", Qualifications [cont] [cont2]);          }          cout << endl;      }      system ("pause");      return 0; }
32 views
In C ++ I made a function that, given a length, creates an array and returns it. Finally I show their values outside the function. I was investigating and they say that in C ++ it is not possible to return an array, and for this there are two alternatives, one is to return a pointer, and another is to pass ... () {     int * arr = create_array (10);     cout << arr [8] << "" << arr [9] << "\ n"; }
16 views
Problem: I know in C++11 they added the feature to initialize a variable to zero as such double number = {}; // number = 0 int data{}; // data = 0 Is there a similar way to initialize a std::vector of a fixed length to all zero's?