C++ :: Two Dimensional Multiplication Table - Dynamic Memory Allocation

Jun 17, 2013

This is the question; Write a function that builds a two-dimensional multiplication table with arbitrary sizes for the
two dimensions.

This is what I have done. I have allowed the user to input whatever size table they want by arbitrarily choosing what value they can input. However I cannot get the board to have blank squares. I thought the char would do it.

Code: #include <iostream>
using namespace std;
char SQAURE_CHAR = {' '};
const int Board_Size = 14;

[Code] ....

View 3 Replies


ADVERTISEMENT

C++ :: Dynamic Class Allocation From A Table

Feb 6, 2012

I want to allocate dynamically classes from a table. There is a different number of class derived from the same father class. Actually I do:

Code:

typedef struct sSysFBList {
TCHAR name[64];
int dim;
SystemFB *fbExec;
}systemFBCalls;

[Code] ....

and so on. NOTE: SystemFB is the virtual father class, all the other are derived from it.

I would like to know if there is the possibility to define the type of the class in the systemFBCalls struct to avoid all these if - else if - else if

Code:
typedef struct sSysFBList {
TCHAR name[64];
int dim;
????? name_of_class;
SystemFB *fbExec;

[Code] .....

View 4 Replies View Related

C++ :: Dynamic Allocation Of Two Dimensional Array With Its Size As Input

Nov 8, 2013

I'm now working on a class to handle matrices and matrix operations. I dynamically allocate a two dimensional array with its size as an input. My problem is that i need to resize the matrix structure (to fit to the size of a product of two matrices or to adjust the size after deleting a row or column for example). I make the resizing by deleting the allocated 2d array after saving its first element address to another pointer to pointer, after saving the useful data in an other array, then i use new[] operator to create a new 2d array using the same address of the old deleted array.

void My_matrix ::delete_row(int a) {
int i,j;
int buffer_row_index = -1; //note that our work is zero based, as we acess the private member of an object of the same class we are working on and we access to the array as it is (zero based) so if this variable is -1, it will be 0 when it enters for the first time and saves the data in the right position.
My_matrix buffer(number_of_rows-1,number_of_columns);

[Code] ....

I do this in a way that i think is unsafe. This code is sometimes unstable and gives an unhandled exception whose reason is unknown. I need to know whether this way is good enough or if there any other better ways.

View 5 Replies View Related

C :: Dynamic Memory Allocation

Jul 14, 2013

I have declared a global variable as pointer. The program performs certain number of iterations. After every iteration, the size of memory required for the pointer changes and this pointer variable is to be accessed by different functions. Now, here is my doubt:If I allocate the memory for this global variable in a function, will the contents of the memory be lost once I exit that function. In my opinion, it should not be the case as the dynamic memory allocation takes place in "heap" and should not be affected by the call of functions.

View 4 Replies View Related

C++ :: Dynamic Memory Allocation (arrays)

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

C++ :: Connect 4 Using Dynamic Memory Allocation?

Jul 3, 2013

I am creating a connect 4 game using dynamic memory allocation the question is;

Write a two-player game of Connect Four where the user can set the width and height of the board and each player gets a turn to drop a token into the slot. Display the board using + for one side, x for the other, and _ to indicate blank spaces.

I have created the board. However I am unsure as how to make a start on getting the players to make moves.

Code:
#include <iostream>
using namespace std;
char **create_table(int width, int height, char blank) {
char **p_p_connect4 = new char*[height];
for(int i = 0; i < height; i++) {
p_p_connect4[i] = new char [width];

[Code]....

View 1 Replies View Related

C++ :: Dynamic Memory Allocation In A Class

Apr 17, 2014

I'm having problems with this code:

#include <iostream>
using namespace std;
class Foo {
public:
Foo( int n );// Constructor
~Foo();// Destructor
int *ptr;
int N;

[Code] ....

I'm using Visual C++ 2008 version. The problem arises at the end, after the sentence 'system("pause")' is reached, which makes me think that the problem happens when calling the destructor. The destructor is called twice, the first time it's called is in the function print. The problem seems to be that the destructor can only be called once.

I know I can avoid this situation by defining the function print like this:

void print ( const Foo &f )
...

but I would like to know if there is some way I can do this keeping the definition that I've provided.

View 2 Replies View Related

C :: Sorting Algorithm With Dynamic Memory Allocation

Nov 17, 2013

I am fairly new to dynamic memory allocation and I keep getting a segmentation fault in this code of mine. This is what the method should do:void sort StringsByReversePoints(char **myWords): This function sorts the char* values (i.e. strings) of myWords in descending order of point value by calling getWordPoints as a helper function and comparing adjacent words. This simple (but inefficient) sorting algorithm starts at the beginning of myWords array and sweeps to the end comparing adjacent values and swapping if they are out of order. After N (length of the array) sweeps the array is fully sorted. Note that efficiency can be improved by a factor of 2 by shortening each successive sweep by one, since the first sweep will have guaranteed the minimum point value word is the last element of the array, the next sweep guarantees the last two elements are correct, and so on....Additionally, if a given sweep results in zero swaps then the array is sorted and you can return immediately.

View 5 Replies View Related

C :: Double Pointers And Dynamic Memory Allocation?

Nov 16, 2013

we are currently covering double pointers and memory allocation. Currently getScrabbleWords is not working. when I compile with commented code (Main() works fine) I get a segmentation fault.

This is the purpose of getScrabbleWords:

char **getScrabbleWords(char **allWords, char letters[]):

This function takes an array of char* values (i.e. strings) representing all the words read from wordlist.txt. Each of these words is tested by callingcanWeMakeIt as a helper function, and pointers to the words that can be made are put into an array, myWords. Note, copies of the words are not made! In order to indicate the end of myWords, we terminate with a NULL pointer. Thus, if N words can be made from letters then myWords should have length N+1.

View 6 Replies View Related

C++ :: Why Cannot Dynamic Memory Allocation Work With References

May 5, 2013

Why cant a dynamic memory allocation work with references? I was told that references work with const pointers deep down so shouldn't this be legal code?

int &&a=new int;

My compiler says that a entity of int* cannot be used to initialize a entity of int&&?

Does that mean that the compiler thinks of them as different types except deep down a reference is implemented with a pointer? Is this right?

View 14 Replies View Related

C++ :: Pointer Incrementing Using Dynamic Memory Allocation?

Feb 13, 2013

I am using a pair of pthreads that call a pair of functions for ping-pong dma data transfer that are used in a loop for data transfer from an acquisition board. For a large # of waveforms, I ultimately run out of PC memory and the program stops. At the end of each function I use the delete[] command to clear memory for reuse, but the pointer appears to advance by the array size used for the transfer until the location exceeds the 2 GB I have for memory. I can see this happening using the Task Manager performance button time plot and window of total memory used continuing to increase to the limit. The culprit for one of the functions (2nd) is:

unsigned char* dataBuffer2 = (unsigned char *) (pci_buffer2.UserAddr);

where pci_buffer1 and 2 have been set up and allocated in main. I also had the following line in each function process:

double* Rin = new double[length];

and it used up memory twice as fast. When I transferred the last line to an area just prior to main and used a constant 1024 for length, the program ran twice as far before exceeding system memory, so it appears that both lines were forcing new memory assignments and moving the pointers accordingly. In addition to using the delete[] command to free memory unsucessfuly at the end of each function procedure, I ended up closing the memory at the end of each procedure, then reallocating it again with the idea that the pointer would be set back to the original value, but it still seems to icrement along. So, neither approach appears to allow reuse of the memory because the pointer continues to march along. Using Visual C++ 6.0 to compile.

View 1 Replies View Related

C++ :: Class Memory Allocation - Store Number Of Elements In Table

Mar 31, 2013

Say I have a class with a few member functions, and only two data members: an int* Table; and an int Size;, to store the number of elements in Table.

I'm using a copy constructor that takes in two parameters: int* table, int size. In this case, is the address that table points to the same address as the object that table is part of? And furthermore, is it possible to say table.Size? I want to compare the passed array's size to the passed size.

View 2 Replies View Related

C :: Dynamic Memory Allocation - Resize Array Of Pointers

May 23, 2013

Suppose I wished to reallocate memory (resize) an array of pointers. Why does the following not work?(The program runs, yet yields a faulty segmentation error message. Why?):

Code: char **ptrarr = (char**) malloc(sizeof(char*))

Code: ptrarr = (char**) realloc(ptrarr, (capacity) * sizeof(char*));

View 14 Replies View Related

C++ :: How To Use Dynamic Memory Allocation And Pointers To Iterate Through The Arrays

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

C++ :: I/O Text File Handling And Dynamic Memory Allocation

May 19, 2014

I am working on an OOP assignment (text handler) which part of its description is:

Some lines of text as a story which the delimiter between each story is %%%%%

Each text loaded should only occupy the space needed for the text to fit.

It's required to use dynamic variables of type char*. To be more detailed, the text-handler must contain a vector of such char-pointers (i.e. c-strings), and the parameter in the constructor indicates how many pointers (c-strings) to be contained in the vector. Each loaded text will be represented by a number, so that the first text in the file gets number 0 and the next one gets number 1 ... etc. When you want to access a text, you request the text with a certain number, and then get a pointer in return that may be used to output the text on the screen.

My problem is first to allocate a dynamic memory like char** without defining the number of array elements (Each text loaded should only occupy the space needed for the text to fit. )and then store each story from text file (comprise of a few lines of text) into that dynamically located memory(char **)to be able to do some operation on it later.

View 15 Replies View Related

C :: Dynamic Memory Allocation - User To Choose Size Of Array

Dec 2, 2013

Dynamic memory allocation in array in c programming. I am trying to make the user to choose the size of array they want to engage in the game.

However, i have remove the global variable which contribute the error to my code previously. Now I assigned all the arr individually but not using the global variable. However, i still not get the desired board i want. i still keep getting 9x9 array board.

And i also need limit the board size only from 4 to 9. And how do i do that.

Code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <malloc.h>
#include <ctype.h>
#include <stdbool.h>

[Code] .....

View 3 Replies View Related

C++ :: Append Comma To A String - Dynamic Memory Allocation Error

May 16, 2012

Trying to append a comma to a string. Getting "Segmentation Error" on Solaris when the function is entered the second time.

Code:

// Appends a comma to the given string
void appendComma(char* instring) {
if (instring == NULL) {
instring = realloc(NULL, strlen(","));
strcpy(instring,",");

[Code] .....

View 14 Replies View Related

C :: Copying File Line By Line Using Dynamic Memory Allocation

Jul 15, 2013

I need to read lines from one file and copy them line by line into another file using dynamic memory allocation. It compiles but gives me a seg fault. Why/How?

Code:
int main(){
FILE *fp1;
FILE *fp2;
FILE *i;
fp1=fopen("file1","r");
fp2=fopen("file3","w+");

[Code] ....

View 2 Replies View Related

C :: Multiplication Table In 2D Array

Dec 30, 2014

I want to declare a 2D 4*4 array, and fills the array with the multiplication table of an integer x from the user, for example: x*1,x*2,x*3, ....., x*16 and how to pass that array to a function to print the array and to another function that swaps a column with another column chosen by the user.

View 3 Replies View Related

C++ :: Multiplication Table Using Functions?

May 25, 2013

how to print a multiplication table of 2,3 ,4 or any table using functions in c++.. ?

Remember using "Functions" e.g Multiplication(int x)
{ }
int main{
then call the function}

View 3 Replies View Related

C/C++ :: Multiplication Table - Formatting?

Mar 31, 2015

I have to build a multiplication table in c++ and Im close but it still isnt formatted properly...

// multiplication table
#include <iostream>
using namespace std;
int main() {
int integer, range;

cout << "Input integer range :"; cin >> integer; cout;
cout << "Input range :"; cin >> range; cout << endl;

[Code] ....

View 7 Replies View Related

C++ :: Multiplication Table In A Void Function?

Jan 20, 2015

I wanted to make a multiplication table.

Here's the code:

#include<iostream>
#include<conio.h>
using namespace std;
void initArray(int arg1[50][50], int r, int c) {
int val=1;
for(int row=0; row<r; row++) {

[code]....

I want the output to be like this:

1 2 3
2 4 6
3 6 9

If the user inputs 3 rows and 3 columns.

View 5 Replies View Related

C++ ::  Dynamic Allocation 2D Array

Nov 14, 2013

I need to confirm that this problem cannot be solved without a pointer. Namely I need to read the rows and columns number from the user cin >> m, n and then use to declare an array int A[m][n];

However as m and n are not constants I am not able to do that. Is there a workaround? The following is the solution I came with BUT using a pointers which should be not the case.

// solution with using pointers as "int A[m][n]" does not work for me!!!
void TwoDimensionalArrayFunc(){
int m = 0;
int n = 0;

// instruct the users to enter array dimensions
cout << "Please insert value for m:";
cin >> m;

[Code] ....

View 6 Replies View Related

C++ :: Dynamic Allocation For Arrays

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

C++ :: Dynamic Allocation From Array?

Dec 26, 2012

I need to write 2 functions:

1. MyMalloc
2. MyFree
___________________________

I have a 1000 bytes global array (which did not dynamic allocated).

I need to make "dynamic allocation" from this array.

For example - MyMalloc(50) ---> The program will allocate 50 bytes OF THE ARRAY'S SIZE.
------
MyFree(pointer) ---> I need to check if the pointer is in the array and free the space.

It should be managed by blocks. The array should also contain the manage variables (for me).

View 19 Replies View Related

C :: Printing Multiplication Table With The Dimension N Given As Input

Oct 26, 2013

I would like to print a multiplication table, with the dimension n given as input. I attached how the table looks like for n=7.

How do you output the character "-" in that sequence? The first and last numbers have 13 "-" characters before and after them, but the numbers in between have 8 "-" characters.

View 2 Replies View Related







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