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


ADVERTISEMENT

C++ :: True / False About Function Parameter

Sep 11, 2014

is it true or false

a function like void myfun(int num){} can receive type "int var" but can't receive type "const int var"

AND

a function like void myfun(const int num){} can receive both type "int var" and also type "const int var"

View 3 Replies View Related

C/C++ :: Palindrome Function Only Return False?

Apr 29, 2015

I have a text file that consists of:

1457887541
Madam
Able was I ere I saw Elba
Straw? No, too stupid a fad. I put soot on warts.
Class is cancelled today

And all of them are returning false as a palindrome and am not sure why that is so.

bool isPalindrome(const char *input){
int first = 0;
int last = strlen(input) - 1;

//begin loop to compare first position with last
while(last > first){

[Code] .....

View 3 Replies View Related

C++ :: Function Return Is Always True?

Oct 2, 2013

This program that I've made works fine to find midpoint, but not distance. The distancefunction always returns a 1 (true). To try to see that it wasn't the math, I added cout.setf(cout.boolalpha) to see and got a result of "true".

//This program is a start to solve basic coordinatre plane distances and midpoints
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

[Code].....

View 3 Replies View Related

C++ :: How To Switch Between Boolean True / False Logic Within Do / While Loop

Mar 25, 2014

I have a hit a snag in a number guessing game program. I was given a half-completed program and told to use functions to complete the missing pieces. It looks unwieldy, but this is how it is supposed to be. I have temporarily made the random guess value visible for troubleshooting purposes.

#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
int welcome() {
cout << " Welcome to the hi-low game!" << endl;

[code]....

The issue lies within this piece of code:

checkGuess(guess, correct); done = false; //either true or false
} while (!done);
cout << "Congratulations, you got it!" << endl;
return 0;
}

I need to manipulate the Boolean variable done so that it registers as false when the user inputs a number higher or lower than the randomly selected value. However, if the user guesses correctly, done will become true and the program will end.

As it stands now, the program will not terminate, and if I set done equal to true like so:

checkGuess(guess, correct); done = true; //either true or false
} while (!done);
cout << "Congratulations, you got it!" << endl;
return 0;
}

Every number the user inputs will register as correct instead of the one right guess.

View 4 Replies View Related

C++ :: Palindrome That Takes A Vector Parameter And Returns True Or False

Apr 26, 2014

Write a function palindrome that takes a vector parameter and returns true or false according to whether the vector does or does not read the same forward as backward (e.g., a vector containing 1, 2, 3, 2, 1 is a palindrome, but a vector containing 1, 2, 3, 4 is not).

Code :
#include <vector>
#include <iostream>
using namespace std;
void palindrome(vector<int>);

[Code] .....

View 4 Replies View Related

C :: Program That Grades A True / False Quiz - Data Available In Text File

Oct 14, 2014

You are to write a C program that grades a true-false quiz. The quiz data will be available in a text file; here is an example:

Quiz #8 (09/22/14) (corrected version)
TTFTTFTFTF
0080 TTFTTFTFTF
0340 TTFTFFTFTF

The first line in the data file is a comment that you may assume can be up to 80 charac- ters long; it cannot be blank. The second line contains the answer key for the 10-question true-false quiz. The following lines in the data file contain a student's id number in column 1 followed by their answers for the quiz in column 2. A 0 (zero) on a line by itself indicates that there are no more students to process.

Write a program that first reads in (from standard input; more on this in a moment) the comment line and answer key as strings followed by each student's data. Store the student's id numbers in an array. You are to "grade" each student's quiz using the key provided fol- lowed by recording their scores (in a second array) in order to be able to print them out later. You do not need to use an array of strings or characters in your program. Write your program allowing for up to 100 students, and you may assume that at least 1 student will have taken the quiz.

You should create test data text files and provide the data to your program using redirection from the command line (see the sample run below). Your program should output the number of students who took the quiz, the average score, the best score, and a table showing each student's id, score, and grade. The formatting, spacing, and labels in your output should 1 match the sample run below exactly.

Your program should determine each student's grade as follows: if the score is equal to the best score b or b−1, give an A. If it is b−2, award a B. Give C's for any score that is equal to b−3 or b−4, a D for b−5 and an F for all others.

Alright So I'm just stuck at comparing the key answer with student answer here is my code comparing one student's answer with the answer key . is that right ?? One more thing for some reason when i try to print answer[0] the result is nothing why is that

Code:
#include<stdio.h>
int main(void) {
char comment[80];
char answer [10];
char studentans [10];
int n=0,i=0,x=0;
int ID[10];

[Code] .....

View 1 Replies View Related

C++ :: How To Create True False And Multiple Choice Questions Using Text Files

Jan 7, 2015

So im trying to create a quiz using c++ that incorporates three different types of questions. These are the standard one answer question, true false questions and multiple choice questions.

How to create the true false and multiple choice questions?

One way that i came up with looks like this

Question A answer1 B answer2;

View 2 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++ :: Count Number Of Vowels Inputted By User - Isvowel Function

Mar 30, 2013

I am trying to create a code that simply counts the number of vowels inputted by the user. Here's where I am.

Code:
#include <iostream>
using namespace std;
bool isVowel(char ch);
int main() {
int count=0;
char character;
int vowelcount=0;

[Code] .....

View 14 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 :: Function To Return First Element Of Linked List Queue

Feb 23, 2013

I am trying to write a function to return the first element of a link list queue. I am not real sure how to implement this. I have include a copy of the struct for my Node & queue.

Code:

typedef struct event_Node {
void *data;
double arri_time;
double serv_time;
double depart_time;
double start_o_serv;

[Code]...

View 4 Replies View Related

C++ :: Binary Search Tree Used With User Defined Class

Nov 29, 2014

I am working on a program that uses a class I created called Student. I want to be able to add different students to a Binary Search Tree, and use the student's gpa (grade point average) to compare students with each other and place them in the correct location in the Tree.

Student class:

#ifndef STUDENT_H
#define STUDENT_H
#include <string>
#include <iostream>
#include <iomanip>
class Student{
friend ostream& operator<<(ostream& out, const Student& stu);

[Code] ......

View 3 Replies View Related

C :: Return Pointer To Largest Element In Array?

Sep 26, 2013

question from chapter 11, qn 8 modern C programming by king

Write the following function: Code: int *find_largest(int a[], int n); When passed an array a of length n, the function will return a pointer to the array's largest element

Code:

#include <stdio.h>
int *find_largest(int a[], int n)
{
int i, x;
x = 0;

[Code].....

have traced the code line by line and it prints correctly up to line 31. However, when the program exits (line 35) , it goes to some other screen with very complex code and my output disappears.

Just to clarify: If I just run the code as is, there is no output. However, when I trace the code line by line, the output appears, but then disappears as the program exits My input was 1 2 3 4 5 6

View 11 Replies View Related

C++ :: How To Return Element In Array That Is Inside A Class

Nov 16, 2014

I have a program where I roll a die X number of times and need to print how many times it lands on each side. I tried to create an array in the class aDie that increments each time the corresponding number is rolled but when I go to call and print it in the main my out put is 0. I just picked how many times it landed on the side 4 just to see if it works and it doesn't.

#define ADIE_H
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
class aDie {

[Code] .....

View 3 Replies View Related

C/C++ :: Dynamic Allocation Of Array And Increase Its Size Every Time New Integer Inputted By User

Aug 29, 2014

I'm a little lost with this program. The idea is to dynamically allocate an array and increase its size every time a new integer is inputted by the user. I believe it is a memory leak but as we have just started learning this I'm not sure how to recognise it. Sometimes I can input as many integers as I want other times 2 or 3 before it crashes. When I can input enough values i exit the loop and send it to the sort function and mean calculator function, all works fine there except the last number inputted becomes this huge value not hexadecimal though... As such I'm at a loss as what to look at next, so here you go:

#include <iostream>
using namespace std;
void SelectSort(int [], int);
float MeanCalc(int [], int);
float MedianCalc(int* [], int);

[Code] .....

View 14 Replies View Related

C++ :: Write Function That Takes Array And Returns True If All Elements In Array Are Positive

Jan 21, 2013

Write a function that takes an array and returns true if all the elements in the array are positive, otherwise, it returns false.

View 6 Replies View Related

C :: Return Number Of Bits Set To True

Jan 3, 2015

The task is to return the number of bits set to true.Here is my code:

Code:

int cardinalityBS(PBitSet _this) {
int s, counter = 0;
for(s = 0; s < 31; s++) {
counter += _this->bits & 1;
_this->bits <<= 1;
}
return counter;
}

[code]....

The code is not working, since whenever I set _this->bits to a number, it returns me the wrong result.

View 7 Replies View Related

C/C++ :: Linear Search Not Found Output

Jul 3, 2014

How to return a message saying that the value searched for is not found. We had to pull the data in from a .dat, i won't let me attach it as a .dat so I attached it as .txt. I know my it's sloppy. I usually clean up what I can once it is working properly.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int ccnt;
int size = 10;

[Code] ....

Attached File(s) : dat45.txt (273bytes)

View 3 Replies View Related

C++ :: Creating Timer - Count By Milliseconds And Return A True Value

Jun 19, 2013

I'm looking to create some sort of a timer. I'd like it to count by milliseconds and return a true value when it counts to a specific time. It would be used something like this:

if(alarm(1000)) {
// do some code
}

I tried using time.h, but it doesn't seem to have any millisecond commands. I need something faster than a second.

View 2 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++ :: Simple Dictionary Search - No Contents Found

Feb 8, 2013

There is no content when i search word in the dictionary.txt....this is my example of dictionary.txt...in the add case it is working,,only in search case...

--------------
dictionary.txt content.
--------------
mwet-asd.
test-test.
apple-prutas.
beryy-berry.
house-house.
--------------

#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
using namespace std;
int counter=0;
char op;

[Code] .....

View 1 Replies View Related

C++ :: Binary Search Delete Function

Nov 26, 2013

I am having an issue when i try to delete a node with 2 children it either doesn't delete anything, or wigs out in various manners deleting the wrong node or replacing a node with a various memory location. As follows, here is the delete function:

void BST::dele(){
bool found = false;//initialize a bool type to "find" the element to be deleted
if(root == NULL) return;//well if the tree's empty, nothing to be found right?
current = root;//set the current to the root to traverse the tree in search of the element
node* parent;//create a parent node for use once the node has been deleted
while(current != NULL){//traverse the tree

[Code] .....

View 1 Replies View Related

C/C++ :: Compare Values Stored In First Array To User Inputted Values In Second Array

Oct 19, 2014

Goal: Write a program that compares the values stored in the first array to the user inputted values in the second array.

In order to fix this error: [URL]...

I had to change my array initialization to one with a star in front of it:

char a1[]={"a","d","b","b","c","b","a","b","c","d","a","c","d","b","d","c","c","a","d","b"};
to:
char *a1[]={"a","d","b","b","c","b","a","b","c","d","a","c","d","b","d","c","c","a","d","b"};

I also changed my 2nd array to one with a star in front of it: char *a2[20];

What does this mean exactly? Putting a star in front of an array?

Also, I am now getting an "unhandled exception" when I try to get input for my 2nd array:

cin>>a2[i];

View 3 Replies View Related

C :: Search And Delete Function For Binary Trees

Apr 1, 2014

I had an assignment that I completed and it was just inserting values into a binary tree and then displaying it. I was wondering what the code would be if I wanted to delete a number in the binary tree, or if I wanted to search for a number. Here is my code for my assignment.

Code:
#include <stdio.h>
#include<stdlib.h>
typedef struct bt_{
int value;
struct bt_ *right;
struct bt_ *left;

[Code] ....

View 1 Replies View Related

C++ :: Binary Search Tree Remove Function

Feb 14, 2014

Im working on a BST remove function. I think I'm on the right track but I'm not sure. From what I understand there are 3 possible cases. A Node with no children, one child, or 2 children(this being the most complex).

void BST::remove(int x) {
TreeNode *n;
TreeNode *v;
n= root;
while(n != NULL && n->key != x){

[Code] ....

View 4 Replies View Related







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