Need help getting started with arrays

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.

Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#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.
Last edited on
Oh, thanks.
It should look something like this:
Enter 10 integers:
1. 23
2. 45
3. 49
4. 23
.
.etc


Enter search value: 23
23 found at index 1
23 found at index 4
Total occurrences is 2


Oh i don't know know any C at all. I'm totally new at this.
I get the headers, but which functions would i have to change?

Thanks for the help btw
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// #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;
}


Most everything remains pretty much the same.
Last edited on
Alright so I tried to compile and got 1 error:

32:15: error: statement cannot resolve address of overloading function

Appreciate the help! :)
What C++ compiler are you using?

Anyways, the code works fine.
http://cpp.sh/45nae7
I'm using putty.

#include <iostream>

using namespace std;

int searchArray(int arr[], int target)
{
int x = 0;
int counter = 0;

while(x < 10)
{
if(arr[x] == target)
{
counter += 1;
}
}
return counter;
}

int main()

{
int x[10];
int counter = 1;


while(counter < 11)
{
cout << "Enter value #" << counter << ":";
cin >> x[counter - 1];
counter += 1;
endl;
}


int targetValue = 0;

cout << "Enter target value: ";
cin >> targetValue;
int total = searchArray(x, targetValue);

cout << "Total \"" << targetValue << "\"s found: " << total;
cin >> counter;

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.
Last edited on
Now I have another problem. After I enter a target value and press enter, nothing happens and I'm in an endless loop.

Did the code from lmsmi1 cause that problem?
Yes, I used the same code...

EDIT: Nevermind, I rewrote the code.
Not sure what I did wrong before but I got it to work this time.

Thanks a bunch! Really appreciate it.
Last edited on
Topic archived. No new replies allowed.