• Register
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.

Could you please help me to set the array for a real quick? Thanks in advance for your kind help.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

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!

13 9 6
94,260 points

Related questions

0 votes
1 answer 3 views
3 views
Problem I want to initialize an array in a same value, how can i do it? Anyone suggest me.
asked Apr 10 Subhendu 4.5k points
0 votes
1 answer 5 views
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!
asked Mar 28 Code Learner 9.5k points
0 votes
1 answer 2 views
2 views
Problem: When an array of objects is declared but not initialized, how can I tell if the array values are null?
asked 4 days ago jamuna1 23.1k points
0 votes
1 answer 13 views
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; }
asked Feb 7 sasha 16.2k points
0 votes
1 answer 21 views
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; }
asked Aug 17, 2020 game 4.6k points
0 votes
1 answer 32 views
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"; }
asked Aug 16, 2020 game 4.6k points
0 votes
1 answer 16 views
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?
asked Dec 26, 2020 Mashhoodch 13k points
0 votes
1 answer 21 views
21 views
Problem: I was trying to create a script that would read decimal numbers and convert them into binary by storing them in a vector with the library <vector> #include <iostream> #include <vector> #include <string.h> using namespace std; int main () { ... 217it returns me☻101100 Specifically, whatever number you type always comes out with either the 02 or the 16th character. Why is this happening?
asked Nov 5, 2020 sasha 16.2k points
0 votes
1 answer 2 views
2 views
Problem: How can I passing array to function in c++?
asked 15 hours ago Humaira ahmed 23.4k points
0 votes
1 answer 1 view
1 view
Problem: How do we pass an array as an argument to a function?
asked 16 hours ago Humaira ahmed 23.4k points