i everyone im looking for some recomendations on what i should learn next in c++. Ive been programming for a about a few months now and I know most of the basics like arrays, pointers , loops etc.. do you think i should learn data structures or algorithms next? or i need to learn some other things before learning algorithms? to get a better understanding of my skill i wrote that code that i wrote mysel
#include<iostream>
usingnamespace std;
int n;
int arr[100];
int i;
int search(int arr[], int n, int val)
{
for (int i= 0; i < n; i++)
{
if (arr[i] == val)
{
bool found = true;
returntrue;
}
else
{
bool found = false;
returnfalse;
}
}
}
int main()
{
int val;
bool found;
cout << " enter number of elements " << endl;
cin >> n;
for (i = 0; i < n; i++)
{
cout << "enter elelement" << i + 1 << " " << endl;
cin >> arr[i];
}
cout << "enter element to search for" << endl;
cin >> val;
found = search(arr, n, val);
if (found)
{
cout << "foundd";
}
else
{
cout << "not found";
}
}
please let me know if you guys haveany recomendation or suggestions. Thanks everyone!
Did you actually compile and test your program? I doubt it will truly produce your desired results. Looking at your program I think you need to continue learning the basics of the language. You really don't yet have a very good grasp of the basic building blocks of the language. Perhaps starting with something like: http://www.cplusplus.com/forum/articles/12974/
Perhaps you may consider actually learning to use some of the basic C++ features like vectors instead of arrays, C++ strings, to name a few.
thanks for the suggestions! i tried the programm and it gives me the correct results. I haven learned about the difference between global variables and local varibales yet. Ive done some of the excersies from the above link and they seem to work. If you have any other sources of where i can practice let me know. Thanks!
i tried the programm and it gives me the correct results.
Maybe if the match is always involving the first element of the array, but I doubt it will work when the matching element is in another element. In your search function that loop will only run one time so it can never check the entire array.
I haven learned about the difference between global variables and local varibales yet.
Then that seems like something you need to research and do something about.
Ive done some of the excersies from the above link and they seem to work.
Have you done them all? By yourself with no help? Have you had anyone review the programs you wrote for those exercises?
I have checed with multiple elements and involving different elements of the array and it always gives the correct result.
I think the loop just searches through the array once and if it finds the value entered by the user it returns true.
I did everything except for graduation and dungeon crawl. Im learning by myself so I dont have much help. But it took me multiple attempts to complete each assignment. here is one with the strings.
I have checed with multiple elements and involving different elements of the array and it always gives the correct result.
Really, what about the following? Shouldn't it return true (found)?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
enter number of elements
5
enter elelement1
1
enter elelement2
3
enter elelement3
8
enter elelement4
88
enter elelement5
5
enter element to search for
88
not found
I think the loop just searches through the array once and if it finds the value entered by the user it returns true.
No, it looks at the first element, then returns either true of false, it never checks any other element.
yeah it returns false in the following example .. But I took your advice and got rid of all the unessasary global variables and changed the code to the following and now it works
#include<iostream>
usingnamespace std;
bool search(int Arr[], int n, int VAL);
int main()
{
int Arr[100], n, val;
bool found;
cout << "Enter number of elements you want to insert ";
cin >> n;
for (int i = 0; i<n; i++)
{
cout << "Enter element " << i + 1 << ":";
cin >> Arr[i];
}
cout << "Enter the number you want to search ";
cin >> val;
found = search(Arr, n, val);
if (found)
cout << "\nItem found";
else
cout << "\nItem not found";
return 0;
}
bool search(int Arr[], int n, int VAL)
{
for (int I = 0; I<n; I++)
{
if (Arr[I] == VAL)
returntrue;
}
returnfalse;