So I have to write a program that allows the user to populate an array of integers with ten values. After the user has entered all the values, then display a prompt asking the user for a target value. Then i must make a function call to the function SearchArray which will receive as input an array argument, number of elements in the array, and a target value to search for. The function has to look through the array for the target value and find a match and display the index location where the value was found. After they have been displayed I must return the total number of occurrences back to the caller.
Just need this simplified and help getting it running possibly.
#include <stdio.h>
int searchArray(int arr[], int target) {
// Array element specifier
int x = 0;
// target value counter
int ctr = 0;
// A for loop could be used as well
// Assuming the number of elements in arr is 10;
while(x < 10) {
if(arr[x] == target) {
ctr += 1;
}
x += 1;
}
return ctr;
}
int main() {
int x[10];
int ctr = 1;
while(ctr < 11) {
printf("Enter value #%i: ", ctr);
scanf("%i", &x[ctr - 1]);
ctr += 1;
printf("\n");
}
int targetValue = 0;
printf("\nEnter target value: ");
scanf("%i", &targetValue);
int total = searchArray(x, targetValue);
printf("Total \'%i\'s found: %i", targetValue, total);
scanf("%i", ctr);
return 0;
}
I attempted to get as close to what I assume you are expected to turn in but am not entirely sure about what you mean by "index location". Did you mean the array element number or memory address or something else?
EDIT: That's all in C, if you're required to use C++ functions/headers change everything as needed.
That C code should work, but for conversion to C++ just change printf() to cout and scanf() to cin, and also include <iostream> instead of the C Standard Library's <stdio.h> like so:
// #include <stdio.h>
#include <iostream>
int searchArray(int arr[], int target) {
// Array element specifier
int x = 0;
// target value counter
int ctr = 0;
// A for loop could be used as well
// Assuming the number of elements in arr is 10;
while(x < 10) {
if(arr[x] == target) {
ctr += 1;
}
x += 1;
}
return ctr;
}
int main() {
int x[10];
int ctr = 1;
while(ctr < 11) {
//printf("Enter value #%i: ", ctr);
std::cout << "Enter value #" << ctr << ":";
//scanf("%i", &x[ctr - 1]);
std::cin >> x[ctr - 1];
ctr += 1;
//printf("\n");
std::cout << std::endl; // -or- \n;
}
int targetValue = 0;
//printf("\nEnter target value: ");
std::cout << "Enter target value: ";
//scanf("%i", &targetValue);
std::cin >> targetValue;
int total = searchArray(x, targetValue);
//printf("Total \'%i\'s found: %i", targetValue, total);
std::cout << "Total \"" << targetValue << "\"s found: " << total;
//scanf("%i", ctr);
std::cin >> ctr; // just for pausing the console if not running from cmd
return 0;
}
}
This is what I'm trying to compile and I'm getting that error.
EDIT: Okay so I got it to compile. I guess I tried to end a line where I shouldn't have.
Now I have another problem. After I enter a target value and press enter, nothing happens and I'm in an endless loop.