[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how to search array for  how many times a value has occurred c++

Posted on 2011-10-09
3
Medium Priority
?
271 Views
Last Modified: 2012-05-12
hello this sample program ask the user for 10 numbers, and then it ask the user for  a search number. after getting the search number it tells the user what index has the search number for example if user types in the values 1,5,7,7,4,5,6,9,10,11 and selects the search number 7 it will show the user where the search number are...Although i don't know how i can make the code so that it also tells the user how many times the search number has occurred  for example in 7 has occurred  2 times.. it should show "the search number occurred 2 times" ...need some help i have pasted my program below.


#include <iostream>
using namespace std;

int main(void)
{
int values[10];
int i;
int search_value;
cout <<"please enter 10 integer values:"<<endl;

for(i=1; i<=10;i++)
{
	cout <<"#"<<i <<": ";

	cin >> values[i-1];
}

cout <<"Please enter a search value: ";
cin >> search_value;


for(i = 0 ; i<10; i++)
{

if (search_value==values[i])
{
 cout <<"found at index "  <<i <<endl;

}//end if statement


}//end for statement

//this should how many times the search value appeared in the array but i don't know how to code that...
cout <<"total occurrences of value " <<search_value <<"is" ;


}

Open in new window

0
Comment
Question by:businessesatoz
3 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 1000 total points
ID: 36938549
Maintain another integer variable and increment it each time your find a value (i.e. each time you display "xxx found at index..." At the end of your search loop, you can print the value of the new integer variable.
0
 
LVL 6

Accepted Solution

by:
bartvd earned 1000 total points
ID: 36938593
See this code:
#include <iostream>
using namespace std;

int main(void)
{
int values[10];
int i;
int search_value;
int counter=0;
cout <<"please enter 10 integer values:"<<endl;

for(i=1; i<=10;i++)
{
	cout <<"#"<<i <<": ";

	cin >> values[i-1];
}

cout <<"Please enter a search value: ";
cin >> search_value;


for(i = 0 ; i<10; i++)
{

if (search_value==values[i])
{
counter++;

}//end if statement


}//end for statement

cout <<"total occurrences of value " << search_value << " is " << counter ;


}

Open in new window

0
 

Author Closing Comment

by:businessesatoz
ID: 36938627
Thanks for your help..
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question