C++ :: Dynamic Containers With Arrays?
Feb 14, 2015
Im trying to create a function that searches my array for a specific string, and then displays that string and the other content associated with it. I'm basically searching for a keyword within an array that has multiple strings with in each element.
View 4 Replies
ADVERTISEMENT
Jan 4, 2013
I'm writing a program in which I have to use a matrix to represent a file in code. because it's a file, the size of the matrix is undefined, and therefore the matrix has to be dynamic. I found out that my compiler doesn't like dynamic multidimensional arrays, so I was thinking of this matrix as a dynamic (monodimensional) array of other dynamic (monodimensional) arrays. My program (and thus this example) uses unsigned chars.
unsigned char variable=something;
unsigned char**matrix=new unsigned char*[lenghtOfMainArray];
for(int rowNumber=0;rowNumber<lenghtOfArray;rowNumber++)
{
[Code].....
View 2 Replies
View Related
Feb 11, 2013
I am having troubles with dynamic arrays and pointers. All the errors are of that kind. I think when I am assigning malloc to an array we assign to its address.
Code:
/*Create an array of genes of the large matrix*/
gene_t gene,gene1,gene2;
gene=malloc(INITIAL*sizeof(gene2)); Code:
while(...) {
if (gene_num==current_size){
[Code] .....
View 9 Replies
View Related
Oct 1, 2014
Code to allocate the memory for my arrays in the create array function.
#include <iostream>
#include <cstdlib>
using namespace std;
typedef int* IntPtr;
const int NUMLABS = 4;
[Code] ....
View 1 Replies
View Related
Mar 28, 2014
By using operator overloads i need to be able to * two dynamic arrays;
this is what i have so far
MyInt operator* (const MyInt& x, const MyInt& y) {
MyInt Temp;
int carry = 0;
if(x.currentSize > y.currentSize){
for( int i =0; i < y.currentSize; i++)
for(int j = 0; j < x.currentSize; j++)
[Code] ....
View 7 Replies
View Related
Mar 4, 2014
i was trying to make a dynamic array in this form :
int x;
cin>>x;
int ar[x];
my g++ (gcc) compiler on linux refused to create an array without a fixed size , however using the same code on windows on dev-cpp it was complied and executed , also it allowd me to create and use the dynamic array , i thought it was a compiler bug , however when i restarted and returned to g++ it compiled and executed the code although it never did before i tried the code on windows , how can that be and is it dangerous ?
View 5 Replies
View Related
Feb 24, 2014
I have recently bought a copy of "Jumping into C++" and have come to chapter 14 ( dynamic memory location) and have a question.
On page 153-154 an example of dynamic allocation is given for array's of int. How would the code look like for strings or structs ?
The allocation was given by:
Code:
int *growArray (int* p_values, int *size)
{
*size *= 2;
int *p_new_values = new int[ *size ];
for ( int i = 0; i < *size; ++i )
{
p_new_values[ i ] = p_values[ i ];
}
delete [] p_values;
return p_new_values;
}
Sample Code I tried to use this for an array of structs but failed completely....
I used the following struct Code:
struct user{
int days;
string name;
};
and the allocation function (which does not work):
Code:
struct user *growarray (struct user *p_values, int *size) {
*size *= 2;
struct user *p_new_values = new struct user[ *size ];
for ( int i = 0; i < *size; ++i )
[Code].....
View 8 Replies
View Related
Oct 22, 2014
Say I have a class called Play now I have a constructor called Play(int, char, int); I create Play play=new Play[2]()//default const Now i want to initialise different values of play objects using play(int char int) Whats the syntax? play[i](vale, value value); ? Its not working..
View 5 Replies
View Related
Sep 27, 2013
So I'm trying to wrap my head around dynamic arrays, and I've finally figured out how to build one, using
int arraysize = randomnumber;
int* arrayname = new int[arraysize];
Now, how to delete the array. Can I free the whole array from any scope in my program using a simple line like delete arrayname;?
Also, since I'm working in heap, I can't rely on the debugger to show me when I'm doing something wrong, If I create a struct such as :
struct twoints {int one, int two};
and then try to create a dynamic array of said struct using
int arraysize = 5;
twoints* arrayname = new twoints[arraysize];
Am I basically creating an array of 5 two ints?
//is it the same as doing this?
twoints arrayname[5];
View 8 Replies
View Related
Apr 25, 2014
Class with Pointers and Dynamic Arrays
View 2 Replies
View Related
Mar 27, 2014
I have become overwhelmed an frustrated at these current operator overloads!
Currently I still can not get my +,* or istream operators working at all!
#include <iostream>
#include <cstring>
#include "myint.h"
[Code]....
View 1 Replies
View Related
Dec 10, 2013
I started to practice some C++. I use to program in C and a little C++. Anyway, I am writing code that creates a dynamic array. I would like to be able to do something like
galaxyobject[object] -> uniqueid = in the class but I do not think I have it setup right.
Question.
1. Is the code correct?
2. Why can't I use the above line without a compile error or segment fault?
int main() {
cout << "ProteusCore Server" << endl;
// create a galaxy system with a certain amount of objects
galaxy galaxysystem;
galaxysystem.IntializeGalaxy(100);
[Code] ....
I made the code available to see on sourceforge as Proteus 3d Game Engine. It is something I would like to work on.
View 3 Replies
View Related
Jan 31, 2013
Do I really need to create a separate pointer to point to dynamic arrays?
T* temp = new T[capacity_ * 2];
T* tIter = &temp; //Do these need to be here?
T* dIter = &data; //Or can I use *(temp + i) and *(data + i)?
(for unsigned int i = 0; i < size_; i++) {
*(tIter + i) = *(dIter + i);
}
View 7 Replies
View Related
Feb 7, 2012
We are supposed to create a menu with an option to add a car (make, model etc) to inventory. I have the structure format set up as far as adding the car, but I am stuck on how to make it a dynamic array of structures or whatnot (I'm not really sure on what I'm trying to do in the first place!) Basically, the user can choose to add a car and its info in. How do you set up the array to add whatever number of cars is needed? Will it be able to add more cars should the user come back to the program later?
This is the instruction of this portion of the project :
Menu Application
Add a car to inventory
Prompted to add
Make, Model and Year, Color, Miles, Price
Finish (adds the car to the file)
Cancel takes you to the main menu
View 1 Replies
View Related
Jan 29, 2015
I need to create subfunctions to do basic Matrix mathematics (addition, subtraction, etc.) I need to be able to pass bot of my Matrices to subfunctions. I started with the addition sub function and I cant get the code to run. I keep getting expected primary-expression before ']' token error for line 75.
#include <iostream>
#include <stdio.h>
using namespace std;
[Code].....
View 1 Replies
View Related
Apr 23, 2014
I cant get with part of it is wrong, i want to sum every row, too.
// Color Number.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<conio.h>
#include<iostream>
[Code].....
View 1 Replies
View Related
Jul 15, 2013
I have a problem in dealing with dynamic arrays. I have initialized the objects and now i want to print them.
Code: // main
char* namesList[] = {"Brad Shaw","Aimen Adams","Sal Dimitry","Cristi Anreaz","Poala James"};
int idList[]={232,444,135,52,134};
Team t1( namesList,idList,5,"waqas");
t1.Print_team(); My class Team looks something like this:
[Code] ....
The print function does not work. I have to implement the print function without taking any parameters.. How should i do that ?
View 9 Replies
View Related
Dec 21, 2014
I need to use dynamic memory allocation and use pointers to iterate through the arrays that I have already in this program. I am lost, nothing I do works and where to use the pointers. I am just looking for a push in the right direction so I can finish this project and how I can implement pointers in my program.
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <stdio.h>
using namespace std;
[Code]...
View 1 Replies
View Related
May 1, 2013
I'm writing a program with two classes, one that contains a card's rank and suit and the other contains pile of these cards. I'm having trouble when it comes to adding a pile to another pile...here's what I have so far:
class Card //Card class declaration
{
private:
int rank; //invoking rank
char suit; //invoking suit
public:
void setCard(int r,char s); //determines card suit/rank
[Code] ....
I understand that I'm calling my pile arrays incorrectly, but I'm not sure why.
View 1 Replies
View Related
Feb 9, 2014
I'm trying to create a function that uses dynamic allocated arrays instead of vectors because I want to see how they work. Basically, this function asks the user to input how many people they are going to enter followed by their name; then, they enter how many of these people want to register for an ID followed by their phone #.
For example:
"How many customers will you like to enter? " 3 //user inputs 3
Bob Allen //user input
Ellen Michaels //user input
Jane Andrews //user input
[Code].....
View 1 Replies
View Related
Oct 30, 2014
I've been working on an assignment that deals with newspaper advertisements. I have two arrays:
categoryCode (the code for the advertisement's category such as pets, cars, etc) and
numWords (number of words in the ad).
The arrays are dynamic.
My assignment has the category listed each time there is an ad associated with it and then its number of words for that ad. If this were a real-world issue I would want my output to list each category only once despite how many ads were in it, list how many individual ads are in the category, and its total number of words for the ads in that category. When using parallel arrays, is there a way to do this?
Let's say I have categoryCode with three elements {5, 5, 7} and numWords {10, 12, 15}.
How would I make the numWords add together only when the categoryCode values are the same?
I would want the output to print:
Category 5 Ads 2 Words 22
Category 7 Ads 1 Words 15
rather than how my instructor is having us do it like this:
Category 5 Words 10
Category 5 Words 12
Category 7 Words 15
I think I'm getting hung up because my mind is confusing the element's location in the array with the element's actual value and I'm probably making this harder than it should be. How would I compare the categoryCode values in a dynamic array? If it were a fixed size I think I could just compare categoryCode[0] to categoryCode[1] and so on, right? But in this case I'm not sure how I would go about that.
View 6 Replies
View Related
Apr 15, 2013
I'm trying to write a function that takes a 32bit address and a data to store at this address.
I'm wanting to take the 32 bit memory address eg 0x12345678 and split it
into 4 x 2 bytes
12, 34, 56, 78
then each of the 4 entries is at most a 256 entry array.eg
FF, FF, FF, FF
So in this example, 0x12 points to 0x34 in the second array, which points to 0x56 in the third array, which finally points to 0x78 in the last array. This last array holds the actual data.
After successfully doing 0x12345678, say I might get a read for 0x1234AABB. So, the first and second pointers already exist, but I then have to create and write to dynamically created arrays.
The arrays need to have all entries set to NULL so that i know whether to follow the pointers to overwrite a previously entered value or create new arrays and pointers.
It all looks good and simple in the pseudo code I've written up but I'm having trouble coding it. I'm currently trying to deal with the first entry case, ie all array elements are NULL, but I'm getting confused with the pointers and creation of new arrays.
void cpu::store(unsigned int mem_add,unsigned int mem_val) {
int first = (mem_address&4278190080)>>24;
int second = (mem_address&16711680)>>16;
int third = (mem_address&65280)>>8;
int fourth= (mem_address&255);
[Code] .....
A1 has been declared as
int* A1[256] ;
View 3 Replies
View Related
Jul 15, 2014
I'm still fairly new to c++ and am having trouble with dynamic arrays. My task is to create an array for test scores. The user needs to define the size of the array and then input data to later sort and calculate the average.
Below is what I have so far. I am struggling with the user input part. I receive the following errors for the data input of the individual scores:
"error C2108:subscript is not of integral type"
"IntelliSense:expression must have integral or unscoped enum type"
Code :
#include<iostream>
#include<iomanip>
using namespace std;
int main() {
//user input total number of scores
cout<<"Please enter the total number of test scores."<<endl<<endl;
[Code] ....
View 1 Replies
View Related
Feb 21, 2014
I've been working on a project that involves storing pointers to dynamically allocated class objects in an STL list, but trying to run it something's going wrong.
list<Actor*>::iterator it;
for(it = m_actors.begin() ; it != m_actors.end() ; ++it)
{
delete *it;
[Code]....
But it seems like that has a memory leak. Does pop_front() just call the destructor for the object, or will it delete a dynamically allocated chunk of memory? If not, how can I do that deletion to avoid a memory leak?
View 4 Replies
View Related
Jul 1, 2012
I need to create function Sum() that calculates sum between two containers. Code below work fine except function Sum between two containers...
How I should re - write my code that everything work fine.
Condition of exercise is : "Also create a Sum() function that calculates the sum between two iterators. The function then uses the template argument for the iterator type and accepts two iterators, the start- and end iterator"
1>------ Build started: Project: HP2_ex2_iter, Configuration: Debug Win32 ------
1> main.cpp
1>c:all myс++ha level 7solutionlevel 7 homework overview of the standard template libraryhp2_ex2_itermain.cpp(47): error C2275: 'C1' : illegal use of this type as an expression
[Code]...
View 5 Replies
View Related
Jul 18, 2012
So i made an STL compatible container.And to make this work I had to make my own iterator (derived from std::iterator).
What is the portable (if any) and "well behaved" thing to do in case of usage anomalies.such as iterating an iterator too far, or passing an invalid index to a operator[]
Looking at how VC++ does things in something like std::array or std::vector.
Code:
iterator_type& operator+=(difference_type offset)
{// increment by integer
#if _ITERATOR_DEBUG_LEVEL == 2
if (size < index + offset)
{// report error
[Code] .....
lots of names starting with underscores, so it's implementation specific. Is there even a "well behaved" thing to do ? Or is any such work always going to be compiler specific?
View 2 Replies
View Related