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
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.
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.
I am trying to read this xml file. This readFile function does not give me right values. Only to use cstring..no pointers or other libs such as xml libs
i'm implementing a playerclass for a game.. in the game there are multiple player types, weapons ect.. i just wanted to turn my players weapons into a dynamically allocated c_str. once i added my: Destructor, Copy Constructor and Overloaded Assignment Operator. My initial values became corrupted and i cannot fix them.
I have a struct called Array and I'm to create a function to create a dynamic array that's fill with randomly generated integers from 0 to 50 (inclusive) and a function to destroy the array for freeing its memory. Below the code that I have written so far.
Code:
* Struct */ typedef struct {int *pArray; //the dynamic array int length; //the size of the dynamic array}Array; /* Function to create a dynamic array */ Array *initializeArray (int length) {int i; }
Using a for loop, construct two 100 element arrays, x and y, such that element i of x stores the value sin(2*pi*i/100)) and the corresponding element of y stores cos((2*pi*i/100)). Print the values stored in the elements of x and y as you calculate them.
I have attempted to solve it but I'm not sure why the value 0 is only being printed, maybe I haven't assigned sin(2i/100)) and cos((2i/100)) to the arrays properly?
Code: #include<stdio.h> #include<stdlib.h> #include<math.h> int main () {
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){
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 ?
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.
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 )
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..
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.
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); }
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
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;
Is it possible to create a dynamic char array on the fly directly from stdin? I do not want to create a fixed length array before hand and then copy contents of it into a malloc created array.
Code: //[1] char line[MAX1]; gets(line);
[Code]....
I could do either [1](buffer overflow problem) or [2] and then goto [3]. But both will have a problem if the input is more than the size MAX1(use defined).
Is it possible to do something of the effect of readLine() method of BufferedReader class in Java or the Console.readLine in .NET? Is it possible to peek into stdin and see the size of the input and then creat an array of the same exact size?
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 ?
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.
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.