+ 2
Why array in c++ has value even though I haven't added an element.
What is this value I am getting and what is the proper way of accessing elements of an array. pseudocode of my program: declare int nums[10]; for (int i=0; i < sizeOf_nums; i++) { cout << nums[i] << " "; }
17 Antworten
+ 3
Matthew Tangpus yup there are also other methods to get indices .
●By using std::find algorithm
You can use the std::find algorithm, which returns an iterator that points to the target value. It is defined in the <algorithm> header. To get the required index, apply pointer arithmetic, or make a call to std::distance.
For more info ,refer
https://www.techiedelight.com/find-index-element-array-cpp/
+ 1
Matthew Tangpus bro those are garbage values not proper values.
Unlike some programming languages, C/C++ does not initialize most variables to a given value (such as zero) automatically. Thus when a variable is assigned a memory location by the compiler, the default value of that variable is whatever (garbage) value happens to already be in that memory location!
Actually, the results of using an uninitialized variable are undefined. You might get back an unpredictable number, your program may crash, or worse.
Note: be habitual to initialize variable during declaration,
+ 1
Also u are assigning locations wrong way,
Suggestion: don't use 2nd for loop
Do like this
int count = 0;
for(int i=0; i<size; i++){
if(nums[i]==seek){
locations[count]=i;
count++;
}
}
+ 1
Martin Taylor thanks for enlightening me because i am very new to the concepts of c++..
+ 1
But having read Martin Taylor instructions it is applicable to use only one loop.. it was a mistake also in my part hehe.. Thanks friends for correcting me I highly appreciate it.. Happy Coding!!
0
I encountered this problem when I tried to do this task:
Algorithm
1. Enter size of array.
2. Declare an array nums[] and locations[] with its given size.
3. Enter elements in nums[].
4. Enter a number to find in the array nums[].
5. Get indexes of number to seek through looping and use condition to check if number to seek is equal to elements in array.
6. Insert indexes to locations array.
7. Declare count = 0 to check the number of times it appears in the array
8. Print results
In my loop:
for (int i=0; i<size; i++){
for (int j=i; j<i; j++) {
if (nums[i] == seek)
locations[j] = i;
count++;
}
}
Print elements inside locations[]:
cout << count << " times it appears";
for(int i=0; i<size; i++) {
cout << locations[i] << " ";
}
My example input and output:
5
1 2 1 1 3
1
3 times it appears
0 [garbage num] 2 3 [garbage num]
I want to filter this output and only print true values that index location.
Any suggestions for me to solve this task.
0
I opted to create else condition and reinitialized the garabage num to 0.1 to filter the results and display only the locations.
I know there is a right way to do this and I really wanted to know.
0
Matthew Tangpus bro your 2nd for loop ,start and terminating condition conflicts
0
Is there other way to get the list of indices without using array??
0
Ooooh I now get it based on your example.. thanks man.. i really appreciate your help.
0
Here is my final work.. I will try to shortened it once I study other way of getting its indices. Thanks again
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int size;
cout << "Enter how many elements are in the array: ";
cin >> size;
int numbers[size];
cout << "Enter " << size << " values for the array: " << endl;
for (int i = 0; i < size; i++) {
cin >> numbers[i];
}
int loc_Num;
cout << "Enter a target to look for: ";
cin >> loc_Num;
int count = 0;
for (int i = 0; i < size; i++) {
if (loc_Num == numbers[i]) {
count++;
}
}
int locations[count];
count = 0;
for (int i = 0; i < size; i++) {
if (loc_Num == numbers[i]) {
locations[count] = i;
count++;
}
}
if (count != 0) {
cout << loc_Num << " is present " << count << " times in the array.\nThe index values where " << loc_Num << " is present are: " << endl;
for (int i = 0; i < count; i++) {
cout << locations[i] << " ";
}
} else {
cout << "Sorry " << loc_Num << " is not in the array." << endl;
}
}
0
Good ,
Here in ur code u have extra for loop after int count = 0;
0
Hi
0
Hêłp☆mę!Č++🥺 because in the second for loop it will insert the indices/locations of the search number
0
Ohh i see,thanks for correcting me😊