C++ :: How To Search For An Item In The Array

Jun 29, 2014

If the item is in the array. Count how many times it is stored in the array and what are their respective array locations.

How can I add this output in my program.

Here's my code...

int array[15]={28,3,16,4,3,16,5,8,4,12,5,4,8,21,40};
int x=0;
int done=0;
int item;

cout<<"Input an item to be searched:";
cin>>item;

[Code] ....

This should be the output:

Item to be searched: 4
number of occurence : 3
array locations : 3 8 11

View 3 Replies


ADVERTISEMENT

C++ :: Item Search Based On 2 Values For Index?

Apr 4, 2013

I have to be more code specific (I usually try to abstract my code). As you can see I use a lot Qt classes... however they work very similar to standard libraries, so it should be not a problem to look at this question.

class FrameManager {
QList<FRAME> frameData;
public:
int frameID(int grpno, int imgno);
};

[Code] ....

Now I know the example search only the ID and doesn't allow the access of the actual data... this is not the question.

The problem is... I will need to define an AnimationManager class that will collect sequence of frames (identified by the unique couple of value) in animations... so it will need to use a massive search of item from the couple of value (and not ID, unluckly)

I fear if I use an approach similar to the one written in the example (search from the start to the end until the element is found) can be very unefficient.

In the same time the AnimationManager should always know if frame exist or not (if not exist an invisible image will be shown) and if in the meantime changed.

Another problem is that I cannot order the data sequence inside FrameManager (becouse it is expected to mantain the original order, even when it is chaotic).

I tried to take a look around QMap (or std::map) but it is not clear at all how the optimiziation works and how I can use it in my case

I tried also to take a look at the "hash" concept, but for me it is too complex to understand deeply.

So... I am feeling like I am entrapped... I am unable to find a good "exit"...

View 8 Replies View Related

C/C++ :: Find Non-key Item In Binary Search Tree

Apr 16, 2014

I am creating a menu-driven program which creates a small database using a binary search tree. I am having issues with the last method I need for the program. The way it is coded now only gives me blank data when I print it. Here is the exact wording for the requirements for this method:

"Output a complete report to the text file named BillionaireReports.txt in increasing order by billionaire rank, that contains all data fro each record stored in the BST. Note: this is not a simple traversal. You may assume that the rank is never less than 1 and never greater that 500. Do NOT copy the data into another BST, into an array, into a linked list, or into any other type of data structure. Retrieve the data directly from the binary search tree which is stored in order by name."

Here is the method in question:

void BillionaireDatabase::displayRankReport() {
int i = 1;
for(i; i <=500; i++) {
billionaireDatabaseBST.contains(Billionaire& anItem);

[Code] ....

I know how to sort the database by the key, but I'm not sure how to sort by something that is not the key.

Here are the functions I have available via the BST (these were written by the professor)

//------------------------------------------------------------
// Constructor and Destructor Section.
//------------------------------------------------------------
BinarySearchTree();
BinarySearchTree(const ItemType& rootItem);
BinarySearchTree(const BinarySearchTree<ItemType>& tree);
virtual ~BinarySearchTree();

[Code] ....

View 3 Replies View Related

C++ :: Binary Search Or Linear Search For 3D Array

Oct 7, 2014

inputting a search array. I tried putting a binary search but I can't get it to work. everything else works up until I put the value I am searching for in the array, then it just crashes.

How it suppose to work: input 2 coordinates with a value each then it calculates the distance between them then it suppose to let user search the coordinates for a value and state if found which coordinate it is at.

#include "stdafx.h"
#include <iostream>
#include <iomanip> //for setprecision
#include <math.h>

[Code].....

View 3 Replies View Related

C/C++ :: Random Item Selection From Array?

Sep 6, 2014

I need to select a random item from an array but I am going to have more than 1 such array so I created the random value selector as different function. Right now I am getting a correct value for random function but when I try to access the array value associated with that index I get 2 digits which does not make any sense.

#include <iostream>
#include <cmath>
#include <ctime>
using namespace std;
int randFunction(int size);
int main() {
int heapsize;

[code]....

View 3 Replies View Related

C/C++ :: Choosing Random Item From Array?

Apr 27, 2015

I need to finish this program which has to ask for seed, then the user gives 5 friends, and then it will choose who to text randomly and show who did not make the cut.

This all i have, I am stuck and I do not know if it correct.

#include <iostream>
#include <string>
using namespace std;
int main() {
string num_friends[5];
int user_input;

[code]....

View 7 Replies View Related

Visual C++ :: Enable / Disable Menu Item 2 In OnUpdate Handler Of Menu Item 1?

Sep 15, 2014

I have two menu items. When item 1 is disabled, I want item 2 to be disabled as well. In the OnUpdate handler of menu item 1, I have tried to use "t_pMenu = pCmdUI->m_pMenu;", "t_pMenu = pCmdUI->m_pSubMenu;" and "t_pMenu = pCmdUI->m_pParentMenu;" but I always get NULL t_pMenu. How can I achieve this purpose?

Code:
void CDummyView::OnUpdateMenuItem1(CCmdUI* pCmdUI)
{
if(m_bShowMenuItem1) {
pCmdUI->Enable(TRUE);

[Code]....

View 14 Replies View Related

C++ :: Convert Binary Search With Int Array To Use String Array

May 8, 2014

I'm having trouble converting this binary search with an int array to use a string array..

Code:

#include <iostream>
#include <string>
using namespace std;
// Function prototype
int binarySearch(string [], int);

[Code] .....

View 3 Replies View Related

C/C++ :: Making Binary Search In Array?

Jul 15, 2014

i was trying to make a program that will asks the user for 10 numbers.then asks the user to enter integer search key.next,the program should find the value in element.i used linear search.my code is like this:

#include <iostream.h>
#include <conio.h>
int linearsearch(const int [], int, int);

[Code]...

how should i make a program that will uses binary search instead of linear search?

View 4 Replies View Related

C++ :: Search And Find The Shortest Queue And Search After Some Condition?

Mar 7, 2013

I am trying to implement a Task scheduler where i have n number of tasks. The Idea behind my task scheduler is that in a loop of queues of a vector, task should get enqueued to the shortest queue among the loop of queues, which is done by the following code.

#include <vector>
#include <queue>
std::vector<std::queue<int> > q
int min_index = 0;
task t // implemented in the other part of the program

[Code] ....

Next i am trying to extend this paradigm to reduce the overhead time of the scheduler, Instead of searching the shortest queue every time, search after some condition ie. search the shortest queue after 5 tasks gets enqueued to the shortest queue.

i need to do something like this

#include <vector>
#include <queue>
std::vector<std::queue<int> > q
task t // implemented in the other part of the program
while(q[min_index].size()!=q[min_index].size()+5) // check whether current min_index queue's size is increased 5 more times if not goto enqueue

[code].....

View 1 Replies View Related

C :: Simple Search - Any Single Numbers In Array

Jul 13, 2013

I wrote a code which checks if there are any single numbers in an array.e.g.In the next array there is a single number - "3" 4 1 4 3 4 1..Here is an array which doesn't have single numbers in it: 4 1 4 3 3 1

Code:
#include <iostream>
using namespace std;
int single (int arr[], int size)
{
int couple=0;

[Code]....

First I thought to double the number of couples I'll get from my search and find some connection to the length of the array, but it hasn't worked out (signed red).

View 9 Replies View Related

C :: Word Search Program - Filling 2D Array

Oct 5, 2014

I am currently working on writing a word search program. However, I am stuck on reading the used input into the 2-D array. The code I've posted below is only dealing with the user input (I'll work on the word search part once I know i am correctly reading in the user input). I know the coding is bad practice with the use of hexadecimal, and getchar() ect. But I am currently using a microblaze microprocessor and this is just the way microblaze can interpret the information. As for the infinite while loops...that can be changed just trying to figure out how.

My question is how could I change my code to correctly read in the user input into the 2-D array?

Code:
#include "platform.h"#include "xparameters.h"
#include <stdlib.h>
#include <stdio.h>
#define MAX 20
int main() {
char grid[MAX][MAX], word[30];
int i, j, arr[2],num;

[Code] ....

View 10 Replies View Related

C++ :: Search Array Of Strings - How Many Time Word Was Used

Feb 4, 2013

The function should search the array of strings to find how many times the word 'the' was used. but its only returning a 0.

int FindThe (int The, char Paragraph[], char Pattern[]) {
for (int i(0); i < 500; i++) {
if (Paragraph[i] == Pattern[0] {

[Code] ....

View 5 Replies View Related

C++ :: Binary Search Of String In Array Of Objects

Oct 20, 2013

I need to search for a string in an array of objects, this is what I have but it does not seem to work, it always gives me the second string in the array instead of the one that i search for.

void binarySearch(Student S[], string name) {
int first = 0;
int last = 9;
int middle;
int position = -1;
bool found = false;

[Code]...

View 3 Replies View Related

C/C++ :: Use Binary Search To Find A Number In The Array?

Mar 23, 2014

i'm trying to use binary search to find a number in the array but i dont know whats wrong with my code. When l enter a number which DOES exist in the array, everything is ok... but when i enter a number which does NOT exist in the array, i have problem...i cant exit the program, it just continues to run.Here is my code

int main()
{
int FirstPosition;

[Code]....

View 2 Replies View Related

C++ :: Binary Search And Sequential Search Algorithm

Sep 16, 2013

Write a program to find the number of comparisons using the binary search and sequential search algorithms

//main.cpp
#include <cstdlib>
#include <iostream>
#include "orderedArrayListType.h"
using namespace std;
int main() {
cout << "." << endl;

[code]....

View 4 Replies View Related

C++ :: Array Of 20 Integers - Binary Search Algorithm To Locate Same Value

Nov 28, 2014

Write a program that has an array of at least 20 integers. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that uses the binary search algorithm to locate the same value. It should also keep count of the number of comparisons it makes. Display these values on the screen.

/*
search benchmark.cpp
this program searchs through a array of 20 integers
*/

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int array [20];
int count;

[Code] .....

View 1 Replies View Related

C++ :: Search Dynamic Array For A String And Return Indices

Feb 20, 2015

I need it to search a dynamic array which I build from an input file. When it finds the user-input string, I want it to store the line number, and continue searching, and record all lines that contain the user-input string.

Here is a link to my complete main.cpp, header file, and implementation file. The function I am having trouble with is "Bagger::lineSearch"

[URL] ....

View 2 Replies View Related

C++ :: Search In A Vector Array From User Input String?

Sep 30, 2013

How would you search in a vector array from a user input string?

ex: user input : "Hello"

output: search vector array and find the line that has the string "Hello" and output the array "Hello" is on?

View 1 Replies View Related

C/C++ :: Iterate Through Array And Search For A Number That Was Stored By User

Nov 23, 2014

So I have an issue with a homework assignment that I am coding. I am attempting to get a function to iterate through an array and search for a number that was stored in an array by the user. So far I can take the number, get the numbers displayed but in my menuChoice2 function, for some reason the program is not confirming whether or not the number is entered, and is only telling me that the number has not been found, instead of confirming that the number is in the array.

Here is my code thusfar:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// Variables that are Globally declared
int menuChoice = 0;
int usernum[1000] = { ' ' };

[Code] .....

To be clear I am not getting any errors but something is telling me that the error is in the formatting of menuChoice2.

View 14 Replies View Related

C :: Create Word Search By Reading In Characters From A File Into Array

Sep 30, 2013

So the plan is to create a word search by reading in characters from a file into an array. This is my code so far, nowhere near finished but have encountered a problem already:

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ROWS 20
#define COLUMNS 20
int main()
{

[Code]...

My question is to do with the section highlighted in red... I can't really see why it is not printing out my array (wordsearch), it's probably very basic but I have no C experience so am not too confident in what I'm doing.

View 1 Replies View Related

C++ :: Search User Input Number In Array Filled With Random Numbers

Nov 6, 2014

I need to create A program That makes a 12x10 Array Grid Filled With Random Numbers From 0-99.

Then I need To Allow The User To Input A Number Between 0-99 And Then The program will then search through the array and count how many of the users number there is inside the array.

Code:

#include <iostream>
using namespace std;
int main() {
int input;
int number;
int row=0;
int col=0;
int Array [12][10];

[Code] ....

View 1 Replies View Related

C :: Linear Search Script - Calling Array Inside Of User Defined Function?

Jul 24, 2013

im tasked with creating a linear search script using functions on a 10 element array. the elements are to be supplied by the user as is the search target.

I understand how to create the array and gather that information from the user as well as howto set a variable for "target", this is what im calling it. Those two parts are simple enough.

I am not fully understanding the calling of an array in a function as a pointer. i semi understand the use of a pointer and howto call a normal pointer in a function. i also understand that an array is nothing more then a "special" pointer with a set of consecutive address blocks for the size of the array.

My first user defined function is simple enough

Code:
ReadArray(int A[], int size){
int i;
printf("Please enter %d integer numbers separated by spaces:
", size);
for (i = 0; i < size; i++)
scanf("%d", &A[i]);
}

Sso nothing out of the ordinary there. that should be a standard for loop and use of scanf, sadly prof has not covered ssanf or any of the other options so i am stuck using scanf for now. maybe someday down the line in a other program or after this course ill get the chance to learn about better options for gathering data from the user.

I am confused as to my next function:

Code:
void SearchArray(int A[], int target, int size);

I've not written any code here yet as im not sure if i should call the A[], or *A for the first type of the function?

If i call *A do i then use something like this for my search:

Code:
for (*A = 0; *A < size; *A++)
if (*A < target) or use A[] insteadA?

Code:
for (i = 0; i < size; i++)
if (A[i] = target)

View 6 Replies View Related

C++ :: Binary Search Function - Return True If Element Inputted By User Found In Array And False If Not

Nov 9, 2014

I was instructed to write a binary search function which would return true if an element, inputted by the user, was found in the array, and false if it was not. I'm not sure why, but my function always returns false. My code is as follows.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
//binary search function
bool search (int array[], int item)

[Code] ....

Why my code does not fulfill it's purpose???

View 7 Replies View Related

C :: Returning Value For Item To Variables?

Mar 4, 2014

What the code below is not doing is returning a value for item to my variables (item1, item2, etc..) I'm not sure how I can make a function prototype that will ask for input and basically recycle after going through the program to a new value for each item. I also need to input individual tax for each item. I will also post it in this message:

double tax, item,salesTax;
double cost(double);
double CalcSalesTax(double);
main(){
double total,item1, item2, item3, item4, item5, tax1, tax2, tax3,tax4,tax5;

[Code].....

View 13 Replies View Related

C :: Removing Item From A Queue

Aug 17, 2014

I'm having a problem with removing an item from a queue. At first in the debugger I got SIGTRAP but I don't get it anymore but the problem still exists. When you try to remove an item from the queue the first nothing happens. Here's the code below compile it and you see what I'm talking about.

Code:

#include <stdio.h>
#include <stdlib.h>
struct Node {
char let;
struct Node *nextNode;
};

[code]....

View 12 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved