C++ :: How To Account For A Size 0 Vector In Function

Dec 16, 2013

I'm trying to do is write a program that fits to a separate test program. The test program provides different size vectors that my function should try and binary search. If the element is found, the function should return 1, and if the element is not found, it returns -1.

Here is the code:

int binSearch(const vector<double> & data, int elem, int & comps) { {
int beg=data[0];
int end=data[data.size()-1];
int mid=(end+beg)/2;

[Code] ......

The problem is that one of the vectors my function is supposed to binary search is a vector of size 0. I tried to throw in an if statement that would return -1 if the size was == 0, but then the program never fully completed and just kept running. So, how can I account for a size 0 vector in my function?

View 2 Replies


ADVERTISEMENT

C++ :: Passing Vector To A Function - Getting Negative Length And Size

May 22, 2013

I am writing a raytracer, and currently I'm working on creating a bounding volume hierarchy to accelerate the process. To do this, I am first creating a vector that holds each of the objects in the scene, and passing this vector to the constructor for my BVH.

Code:
//in header
BVH_Node* bvh;
//in main raytrace function

[Code] .....

I am testing a scene that has only 2 objects, and so it goes to the size == 2 check. The first time it hits makeLeaf(), I segfault. I've used both gdb and valgrind, and of course it's a memory mapping error. gdb's backtrace tells me that the length of the vector I've passed in is -805305610 and the capacity is -21, and that it is inside my makeLeaf() function that the error occurs.

Here's the function:

Code:
BVH_Node* BVH_Node::makeLeaf(GeomObj* v){
BVH_Node* node;
node->obj = v;
node->isObj = true;
return node;
}

The segfault happens at
Code: node->obj = v;
If I run my raytracer without a BVH, the objList works perfectly.

View 8 Replies View Related

C++ :: Bank Account - Function Not Storing Data In Array

Mar 5, 2014

Following function is from a bank account, when new account is created it won't store it in the array or i guess it does but find_acct function wont be able to find it.

find_acct Function:

int findacct(int acctnum_array[], int num_accts, int requested_account) {
for (int index = 0; index < num_accts; index++)
if (acctnum_array[index] == requested_account)
return index;
return -1;

[Code] ....

View 2 Replies View Related

C++ :: What Is Default Size Of STL Vector

Jan 30, 2012

For example, I have an empty vector of integer. If I keep calling push_back on vector, is it going to be out of memory?

View 8 Replies View Related

C++ :: Vector Reaching Its Maximum Size

Feb 5, 2014

My program enters the size of the vector from the user and then creates a vector of vectors (lets say SIZE1). In addition the user enters the number of vector of vectors he needs (lets say SIZE2) as follows:

class Vectors {
// member functions goes here
private
vector<vector<int>> vectors;
vector<int>::iterator it;

[Code] .....

With a few calculations and insertions to my vector (vector of vectors)... the program works fine and gives me the results...

However, with huge calculations and insertions the program stops working and gives me this message

"Unhandled exception at at 0x770DC41F in Test.exe: Microsoft C++ exception:std:bad_alloc at memory location 0x001CEADC"

Thus, it seems that the vector reached it's maximum size... I tried to use reserve() but did not work

I read that "By default, when you run a 64-bit managed application on a 64-bit Windows operating system, you can create an object of no more than 2 gigabytes (GB). However, in the .NET Framework 4.5, you can increase this limit"

What do you think would be the best option for me to do (note my program is very long and complex)(I'm currently using Microsoft Visual Studio 2012 32Win application):

1. convert my program to the .NET Framework (C++)

2. convert my program to C# in case c#

3. do any settings on my computer (my workstation has a 3.6GHZ xion processor with 32RAM

4. convert to another version of C++ that does not have any restriction on the size of the array (if available)

Please note that I never worked neither with the .NET framework nor C#

View 3 Replies View Related

C++ :: Memory Size Of Vector Elements

Apr 9, 2014

I had a question about memory allocation/how iterators work for a std::vector<foo> of a user defined class 'foo'. Say foo contains variables of variable size, so that each member of the std::vector<foo> does not require the same amount of memory space.

Does c++ allocate the same amount of memory for each element, equal to the amount of memory required for the largest element? Or does it use some sort of array of pointers pointing to the location of each element in the vector to make the iterator work? Or does it use some other method? I am wondering because I wrote a code which reads data from a binary files and stores most of it in std::vectors.

The code seems to be using significantly more memory than the sum of the size of all the binary files, and I am using vectors made up of the datatype within the binary files (float). So I was wondering if internally the code was allocating space for each vector element which is the size of the largest element as a way to handle indexing/iterators. I ran my code through a memory leak checker and it found no errors.

View 16 Replies View Related

C++ :: Initialize Size Of Vector Array Position And Color?

Feb 3, 2015

Getting back into programming after a few years off and a bit rusty.

My question is: Is this going to initialize the size of the vector array's position and color properly?

#include <GLFW/glfw3.h>
#include <vector>
class TerrainClass {
private:
struct VertexType {
std::vector<float> position[3];

[Code]...

View 6 Replies View Related

C++ :: Char Vector - Fixed Size Two Dimension Array

Jun 9, 2013

I want to save the char[8][8] // fixed size 2 dimension array

to a vector

such as

vector<?????> temp;

is there anyway to approach this?

View 4 Replies View Related

C++ :: Using Array Of Size That Will Be Determined By User - Implementing Vector

Oct 20, 2013

I am trying to use an array of a size that will be determined by the user, therefore I must use a vector, right?

In class I was told that this is how I call a vector:

vector <int> x;

Is the vector called vector? Is it called x?

Can I do this?

for(int i=0;i<=10;i++)
{
cout<<x[i];
}

Some basic ways of implementing a vector? How it works or how I can do anything with it.

View 2 Replies View Related

C++ :: Using Vector Push Back Function To Output Contents Of Vector (similar To Array)

Feb 9, 2015

How to output vector contents using the push_back function. My program reads in values just fine, but it does not output anything and I've been stuck on why.

here is my code:

#include <iostream>
#include <array>
#include <vector>
using namespace std;
int duplicate( vector < int > &vector1, const int value, const int counter)

[Code].....

View 3 Replies View Related

C++ :: Recursive Function 2 - Create Vector Of Vector Of Integers

Mar 26, 2013

Lets say that I have a vector of vector of integers. <1,2,3,4> , <5,6,7,8>, <10,11,12,13>

How do I make a function that creates vector of vector of every different integers?

<1,5,10> , <1,5,11>, <1,5,12>, <1,5,13>
<1,6,10> , <1,6,11>, <1,6,12>, <1,6,13>
<1,7,10> , <1,7,11>, <1,7,12>, <1,7,13>
<1,8,10>, <1,8,11>, <1,8,12>, <1,8, 13>
<2,5,10>, <2,5,11>, <2,5,12>, <2,5,13>

and so on...

View 2 Replies View Related

C++ :: Find Function To A Vector Of Structures Vector

Jul 5, 2013

I have asked a related question before, and it was resolved successfully. In the past, when I wanted to use std::max_element in order to find the maximum element (or even sort by using std::sort) of a vector of structures according to one of the members of the structure, all I had to do was to insert a specially designed comparison function as the third argument of the function std::max::element. But the latter comparison function naturally accepts two arguments internally.

For instance, here is a test program that successfully finds the maximum according to just one member of the structure:

Code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

[Code] ....

And the output was this, as expected:
Maximum element S.a of vector<S> vec is at: 9
[I]max element of vec.a between slot 3 and slot 6 is: 6, and its index is: 6 vec[6].a = 6
[I]max element of vec.a between slot 4 and slot 7 is: 7, and its index is: 7 vec[7].a = 7
[I]max element of vec.a between slot 5 and slot 8 is: 8, and its index is: 8 vec[8].a = 8
[I]max element of vec.a between slot 6 and slot 9 is: 9, and its index is: 9 vec[9].a = 9

However, I now need to search and find an element of vector<myStruct> according to just one member of myStruct, instead of finding the maximum or sorting as before. This presents a problem because the function std::find does not accept such a comparison function as its third argument.

This was the description of the std::find function that I found: find - C++ Reference

Code:
template <class InputIterator, class T> InputIterator find (InputIterator first, InputIterator last, const T& val);

I could also find another function called std::find_if, but this only accepts a unary predicate like this: find_if - C++ Reference

Code:
template <class InputIterator, class UnaryPredicate> InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);

And once again this is either inadequate of I don't see how to use it directly, because for the third argument I would like to insert a function that takes two arguments with a syntax like this:

Code:
int x=7;
std::vector<S>::iterator result;
result = std::find(vec.begin(), vec.end(), []( const (int x, const S & S_1) { return ( x == S_1.a ) ; } ) ;

Is there another std function that I can use to make search and find an element according to just one member of myStruct?

Or perhaps there is a clever way to pass two arguments to the unary predicate.

View 4 Replies View Related

C :: This Account ID Does Not Exist - Segmentation Fault

Apr 11, 2013

It is suppose to display the message "This Account ID does not exist" but why do I get "segmentation fault"??I was able to compile and run the program so it shouldn't be a problem of the compiler.

Code:

#include <stdio.h>#include <string.h>
struct Account {
char* Account_ID;
char* Account_Type;
char* AccountOwner_FirstName;
char* AccountOwner_LastName;
float* Balance;

[Code]....

View 11 Replies View Related

C++ :: How To Take Into Account White Spaces In A String

Apr 21, 2013

as i am doing an encryption program on a playfair cipher. I am now stuck on a problem on decryption.If my string is helloworld (without a space), it will decrypt normally.However , if my string has a space in between it. Let`s say Hello World, it will not decrypt normally.How do i take into account the space that is in between hello & world?

example: hello world

View 4 Replies View Related

C# :: Create Account And Login To Windows?

Mar 18, 2014

I want to create user account and login to windows. I have to read username from DB and create window user for login to window. So,Can I create windows account and login to windows with C#

View 8 Replies View Related

C :: Size Of Function And Pointers

Apr 6, 2014

I have this simple program:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static unsigned char cmd[]={
0x01,0x80,0x00,0x00,0x00

[Code] .....

All is right except the size. Why does it give 80x1 as size instead of the digit 5?

View 2 Replies View Related

C :: How To Account For Different Number Of Chars In Array Of Names

Dec 9, 2013

I am trying to make a for loop that will print out chars in an array while using a #define before main. My problem is that each name has a different amount of chars in it. How do you account for that when you are trying to define a size? For example, I am playing around with the numbers and I just put 7 in for size:

Code:
#include <stdio.h>
#define sizeOf 7
//char personName( char * name[] );
char printName ( char * name[]);

[Code] .....

View 8 Replies View Related

C++ :: Filling Vectors With Pointers To Account Objects

Jul 27, 2013

So I'm trying to fill a vector with pointers to account objects.

std::vector<account*> fill_vector() {
std::vector<account*> temp;
std::ifstream s;
std::string str;
s.open("H://account2.dat");

[Code] ....

The accounts have four different types and I am supposed to skip over ones that have an invalid account type and throw an exception:

account* factory(std::string account_code, std::string first_name,
std::string last_name, char type, double balance) {
try {
if(type == 'A') {
simple_account *a = new simple_account(account_code, first_name, last_name, balance);

[Code] .....

My problem is they program will not skip over my rejected accounts.. still adds them to the vector but I cant figure out why!

View 8 Replies View Related

C/C++ :: Calculating Interest Earned On Bank-account

Oct 21, 2014

This exercise should familiarise you with loops, if-then-else statements, and recursion. You will have to design and implement a program that calculates the interest earned on a bank-account. Deadline is the end of Tuesday in week 4.

Interests are compounded; that is, you earn interest on interest. Given a yearly interest rate of say, 6%, you can calculate the total sum available when an initial sum of 4000 pounds is put away for 13 years as follows:

4000 * ( (1 + [6/100])^13 ) = 8531.71 pounds

where the caret symbol denotes 'to the power of'. One way to calculate the power is by repeatedly mulitplying. Ie, you can mulitply 1.06 with 1.06 12 times to calculate 1.06^13.

PART 1

Design and implement a function that raises X to the power Y for a real number X and a positive integer Y. The function must use a while loop.

Design and implement a main program that calculates the sum availble when 1000 pounds has been put away for 25 years with 5 percent interest.

Change the while-loop in the function to a for-loop.

View 12 Replies View Related

Visual C++ :: Building A Savings Account Program?

Mar 12, 2014

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

[Code].....

I need to modify the program to use annual interests rates of 3%, 4%, and 5%.

After that I need to modify the program to make it so the user enters the dollar amount of the deposit, the interest rate or rates, and the number of years.

I'm a design student at New England Tech but before we can go full design they want to make sure we know for sure if we want to be a designer or a programmer and so we need to take C++ classes

View 4 Replies View Related

Visual C++ :: How To Tell If User Account Is Set Up With Roaming Profile

Nov 22, 2013

Is there a way to know from a Windows service application (running as a local-system) if a specific logged in interactive user account is configured with a roaming profile? I need this to be compatible with Windows XP SP3.

View 9 Replies View Related

C :: Function That Returns ARRAY And Its SIZE?

Dec 24, 2013

to return the array i shall make a pointer function thats ok.. but how do I get the size return if i dont know the size?

if I need to make AXB=C and output C my new array doesnt have a size..

View 7 Replies View Related

C++ :: Function Argument As Array Size?

Feb 22, 2013

I made a function like follows:

void foo(const double va, const int q) {
int qaa[q];
......
return;
}

However, the compiler indicates allocator cannot allocate an array of constant size 0... how can I use the argument "q" to fix the size of array "qaa"?

View 8 Replies View Related

C++ :: Mini Bank Account System - Using Header Files?

Feb 13, 2013

i am currently doing a mini bank account system using visual studio...i need using the header files...the question requires to use 3 header files for each class.these are the classes:

a)Accounts
b)CurrentAccounts
c)SavingAccounts

i have done the first part... how to do a new class in a different header file..

View 3 Replies View Related

C++ :: Bank Account - Base Class Undefined Error

Apr 20, 2014

I have an odd compiling error. My base class is all delcared and read to go. But I could not figure out how to get my sub classes inherit from it. So I had to make everything public. Even inheriting using the protected inheritance thingy, still could not get them to inherit from that base class.

So I compiled the program, and no other class came back with an error. only my base class. Here is the base class:

#include <iostream>
#include <string>
using namespace std;
class Bankaccount {

[Code] .....

The error I'm getting is saying:

error C2143: syntax error : missing ';' before 'using'
Where is there anything needing a semicolon before using?

'Bankaccount' : 'class' type redefinition
see declaration of 'Bankaccount'

Is it saying this because I don't have any private class members?

'Bankaccount' : base class undefined

But it is defined.

What is the issue with this class? And how can I get it to compile and run?

View 10 Replies View Related

C++ :: Calculate Balance Of Saving Account At The End Of Three Month Period

Dec 11, 2014

(I'm using visual c++). Write a program that calculates the balance of a savings account at the end of a three month
period. It should ask the user for the starting balance and the annual interest rate. A loop should then iterate once for every month in the period, performing the following:

A) Ask the user for the total amount deposited into the account during that month. Do not accept negative numbers. This amount should be added to the balance.

B) Ask the user for the total amount withdrawn from the account during that month. Do not accept negative numbers or numbers greater than the balance after the deposits for the month have been added in.

C) Calculate the interest for that month. The monthly interest rate is the annual interest rate divided by 12. Multiply the monthly interest rate by the average of that month’s starting and ending balance to get the interest amount for the
month. This amount should be added to the balance.

After the last iteration, the program should display a final report that includes the following information:

• starting balance at the beginning of the three-month period.
• total deposits made during the three months
• total withdrawals made during the three months
• total interest posted to the account during the three months
• final balance.

What I need is to solve that particular problem using a class. Here is the code solved for the problem not using a class:

#include<iostream>
using namespace std;
int main() {
double annulInterestRate,
balance,
monthlyInterestRate,

[Code] .....

View 8 Replies View Related







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