Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back.
By the way, my problem is like this i the first program was compiled and run without any erros but the second program has a run time error when the function return from allocate and the ptr become NULL. How to fixed this?
Second Program: -
-
/* Best Method to allocate memory for 2D Array because it's
-
much more flexible
-
*/
-
-
/* int ***matrixptr -> **rowptr -> *rowptr[nrow] -> element of row
-
1st Matrix -> *rowptr[nrow] -> element of row
-
-> *rowptr[nrow] -> element of row
-
-
-> **rowptr -> *rowptr[nrow] -> element of row
-
2nd Matrix -> *rowptr[nrow] -> element of row
-
-> *rowptr[nrow] -> element of row
-
-
-> **rowptr -> *rowptr[nrow] -> element of row
-
3nd Matrix -> *rowptr[nrow] -> element of row
-
-> *rowptr[nrow] -> element of row
-
*/
-
// How to code this in C
-
-
#include<stdio.h>
-
#include<stdlib.h>
-
#include<assert.h>
-
-
/* int **rowptr -- A pointer to pointer to integer
-
This can be explain through graphically.
-
int **rowptr -> *rowptr[nrow] -> element of row
-
-> *rowptr[nrow] -> element of row
-
-> *rowptr[nrow] -> element of row
-
*/
-
static void Allocate(int, int **, int **, int *, int *, int *, int *);
-
//int Symmetric(int *, int *, int *, int *);
-
-
void Add(int **, int **, int *, int *);
-
void Subtract(int **, int **, int *, int *);
-
-
void Display(int **, int *, int *);
-
// int *nrow_ptr = NULL, *ncol_ptr = NULL;
-
-
/* int *nrow_ptr, *ncol_ptr;
-
// Declare the nrow_ptr and ncol pointer
-
nrow_ptr = &nrow;
-
ncol_ptr = &ncol;
-
// Initialized the nrow_ptr and ncol_ptr
-
*/
-
-
-
int main(int argc, char *argv[])
-
{
-
static int **rowptr;
-
static int **rowptr_1;
-
static int **result;
-
-
int nMatrix;
-
int nrow, ncol, nrow_1, ncol_1;
-
// Number of row and column
-
int *nrowptr;
-
int *ncolptr;
-
int *nrow_1ptr;
-
int *ncol_1ptr;
-
// Initialized the pointer pointing to nrow, ncol,
-
// nrow_1, ncol_1 respectively
-
nrowptr = &nrow;
-
ncolptr = &ncol;
-
nrow_1ptr = &nrow_1;
-
ncol_1ptr = &ncol_1;
-
/*
-
Used while loop instead of dowhile because it's recommended
-
by Bjarne Stroustrup
-
*/
-
// ------------------------------------------------------------
-
while(1)
-
{ // Must allocate at least two matrix
-
printf("\nPlease Enter same dimension for two Matrix\n");
-
printf("\nHow many Matrix : ");
-
scanf("%d", &nMatrix);
-
-
printf("\nHow many Row for First Matrix : ");
-
scanf("%d", &nrow);
-
printf("\nHow many Column for First Matrix : ");
-
scanf("%d", &ncol);
-
-
printf("\nHow many Row for Second Matrix : ");
-
scanf("%d", &nrow_1);
-
printf("\nHow many Column for Second Matrix : ");
-
scanf("%d", &ncol_1);
-
// ------------------------------------------------------------
-
Allocate(nMatrix, rowptr, rowptr_1, nrowptr, ncolptr, nrow_1ptr, ncol_1ptr);
-
if (rowptr || rowptr_1 == NULL)
-
{
-
perror("DMA for rowptr and rowptr_1 became NULL when came back from allcoate function");
-
}
-
Add(rowptr, rowptr_1, nrowptr, ncolptr);
-
// Adding the element of matrix
-
Subtract(rowptr, rowptr_1, nrowptr, ncolptr);
-
// Subtract the element of matrix
-
// Display(result, nrow, ncol);
-
// Display the result after successful allocated the memory
-
-
free(rowptr);
-
free(rowptr_1);
-
free(result);
-
}
-
-
-
return 0;
-
-
}
-
// -----------------------------------------------------------
-
static void Allocate(int nMatrix, int **rowptr, int **rowptr_1,
-
int *nrowptr, int *ncolptr,
-
int *nrow_1ptr, int *ncol_1ptr)
-
{
-
// int symmetric;
-
int row_loop, col_loop;
-
-
switch(nMatrix)
-
{
-
case 1:
-
{
-
rowptr = (int **)malloc(sizeof(int **) * (*nrowptr));
-
if (rowptr == NULL)
-
{
-
perror("Dynamic Memory Allocation for row Fails");
-
}
-
-
for (row_loop = 0; row_loop < (*nrowptr); row_loop++)
-
{
-
rowptr[row_loop] = (int *)malloc(sizeof(int *) * (*ncolptr));
-
if (rowptr[row_loop] == NULL)
-
{
-
perror("Dynamic Memory Allocation for column Fails");
-
}
-
}
-
-
for (row_loop = 0; row_loop < (*nrowptr); row_loop++)
-
{
-
for (col_loop = 0; col_loop < (*ncolptr); col_loop++)
-
{
-
printf("Enter the [%d][%d] number : ", row_loop, col_loop);
-
scanf("%d", &rowptr[row_loop][col_loop]);
-
}
-
}
-
break;
-
}
-
// ------------------------------------------------------------
-
case 2:
-
{ // Enter the first matrix
-
// do
-
// {
-
printf("\nEnter the First Matrix : \n");
-
// Allocate memory for rowptr **
-
// if nrow == 3, then same as rowptr *[3]
-
rowptr = (int **)malloc(sizeof(int **) * (*nrowptr));
-
if (rowptr == NULL)
-
{
-
perror("Dynamic Memory Allocation for row Fails");
-
}
-
-
// For each rowptr * , allocate ncolumn
-
for (row_loop = 0;row_loop < (*nrowptr); row_loop++)
-
{
-
rowptr[row_loop] = (int *)malloc(sizeof(int *) * (*ncolptr));
-
if (rowptr[row_loop] == NULL)
-
{
-
perror("Dynamic Memory Allocation for column Fails");
-
}
-
}
-
-
for (row_loop = 0; row_loop < (*nrowptr); row_loop++)
-
{
-
for (col_loop = 0; col_loop < (*ncolptr); col_loop++)
-
{
-
printf("Enter the [%d][%d] number : ", row_loop, col_loop);
-
scanf("%d", &rowptr[row_loop][col_loop]);
-
}
-
}
-
-
// -----------------------------------------------------------
-
// Enter the second Matrix
-
printf("Enter the Second Matrix : \n");
-
-
// Allocate memory for rowptr_1 **
-
// if nrow == 3, then same as rowptr_1 *[3]
-
rowptr_1 = (int **)malloc(sizeof(int **) * (*nrow_1ptr));
-
if (rowptr_1 == NULL)
-
{
-
perror("Dynamic Memory Allocation Matrix 2 for row Fails");
-
}
-
-
// For each rowptr_1 * , allocate ncolumn
-
for (row_loop = 0; row_loop < (*nrow_1ptr); row_loop++)
-
{
-
rowptr_1[row_loop] = (int *)malloc(sizeof(int *) * (*ncol_1ptr));
-
if(rowptr_1[row_loop] == NULL)
-
{
-
perror("Dynamic Memory Allocation Matrix 2 for column Fails");
-
}
-
}
-
-
for (row_loop = 0; row_loop < (*nrow_1ptr); row_loop++)
-
{
-
for (col_loop = 0; col_loop < (*ncol_1ptr); col_loop++)
-
{
-
printf("Enter the [%d][%d] number : ", row_loop, col_loop);
-
scanf("%d", &rowptr_1[row_loop][col_loop]);
-
}
-
}
-
-
// Check the Dimension of Two Matrix
-
/* symmetric = Symmetric(nrowptr, ncolptr, nrow_1ptr, ncol_1ptr);
-
if (symmetric == 1)
-
{
-
printf("The two Matrix is same dimension");
-
}
-
else
-
{
-
printf("The two Matrix is different dimension");
-
}
-
}while(symmetric == 0); */
-
break;
-
}
-
}
-
}
-
// ------------------------------------------------------------
-
int Symmetric(int *nrowptr, int *ncolptr,
-
int *nrow_1ptr, int *ncol_1ptr)
-
{
-
int symmetric;
-
if ((*nrowptr) == (*nrow_1ptr) && (*ncolptr) == (*ncol_1ptr))
-
{
-
symmetric = 1;
-
return symmetric;
-
}
-
else
-
{
-
symmetric = 0;
-
return symmetric;
-
}
-
}
-
// ------------------------------------------------------------
-
void Add(int **rowptr, int **rowptr_1, int *nrowptr, int *ncolptr)
-
{
-
static int **result;
-
int row_loop, col_loop;
-
-
// Allocate memory for result **
-
// if nrow == 3, then same as result *[3]
-
result = malloc(sizeof(int **) * (*nrowptr));
-
if (result == NULL)
-
{
-
perror("Dynamic Memory Allocation result for row Fails");
-
-
}
-
// For each result * , allocate ncolumn
-
for (row_loop = 0; row_loop < (*nrowptr); row_loop++)
-
{
-
result[row_loop] = malloc(sizeof(int *) * (*ncolptr));
-
if (result[row_loop] == NULL)
-
{
-
perror("Dynamic Memory Allocation result for column Fails");
-
}
-
}
-
// Adding the element of two matrix
-
for (row_loop = 0;row_loop < (*nrowptr); row_loop++)
-
{
-
for (col_loop = 0; col_loop < (*ncolptr); col_loop++)
-
{
-
*(*(result + row_loop) + col_loop) = *(*(rowptr + row_loop) + col_loop) + *(*(rowptr_1 + row_loop) + col_loop);
-
}
-
}
-
-
Display(result, nrowptr, ncolptr);
-
}
-
-
// ------------------------------------------------------------
-
void Subtract(int **rowptr, int **rowptr_1, int *nrowptr, int *ncolptr)
-
{
-
int row_loop, col_loop;
-
static int **result;
-
-
// Allocate memory for result **
-
// if nrow == 3, then same as result *[3]
-
result = malloc(sizeof(int **) * (*nrowptr));
-
if (result == NULL)
-
{
-
perror("Dynamic Memory Allocation result for row Fails");
-
-
}
-
// For each result * , allocate ncolumn
-
for (row_loop = 0; row_loop < (*nrowptr); row_loop++)
-
{
-
result[row_loop] = malloc(sizeof(int *) * (*ncolptr));
-
if (result[row_loop] == NULL)
-
{
-
perror("Dynamic Memory Allocation result for column Fails");
-
}
-
}
-
// Subracting the element of two matrix
-
for (row_loop = 0; row_loop < (*nrowptr); row_loop++)
-
{
-
for (col_loop = 0; col_loop < (*ncolptr); col_loop++)
-
{
-
*(*(result + row_loop) + col_loop) = *(*(rowptr + row_loop) + col_loop) - *(*(rowptr_1 + row_loop) + col_loop);
-
}
-
}
-
Display(result, nrowptr, ncolptr);
-
}
-
-
// ------------------------------------------------------------
-
void Display(int **result, int *nrowptr, int *ncolptr)
-
{
-
int row_loop, col_loop;
-
// Loop for row and column
-
-
for (row_loop = 0; row_loop < (*nrowptr); row_loop++)
-
{
-
for (col_loop = 0; col_loop < (*ncolptr); col_loop++)
-
{
-
printf("\nThe value of array at [%d][%d] is %d", row_loop, col_loop, *(*(result + row_loop) + col_loop) );
-
}
-
}
-
-
}
-
-
// ------------------------------------------------------------
-
Thanks for your help.
Your help is greatly appreciated by me and others.
1 7940
The first program : -
-
/* Best Method to allocate memory for 2D Array because it's
-
much more flexible
-
*/
-
-
/* int ***matrixptr -> **rowptr -> *rowptr[nrow] -> element of row
-
1st Matrix -> *rowptr[nrow] -> element of row
-
-> *rowptr[nrow] -> element of row
-
-
-> **rowptr -> *rowptr[nrow] -> element of row
-
2nd Matrix -> *rowptr[nrow] -> element of row
-
-> *rowptr[nrow] -> element of row
-
-
-> **rowptr -> *rowptr[nrow] -> element of row
-
3nd Matrix -> *rowptr[nrow] -> element of row
-
-> *rowptr[nrow] -> element of row
-
*/
-
// How do code this in C
-
-
#include<stdio.h>
-
#include<stdlib.h>
-
#include<assert.h>
-
-
-
/* int **rowptr -- A pointer to pointer to integer
-
This can be explain through graphically.
-
int **rowptr -> *rowptr[nrow] -> element of row
-
-> *rowptr[nrow] -> element of row
-
-> *rowptr[nrow] -> element of row
-
*/
-
int Symmetric(int, int, int, int);
-
void Add(int **, int **, int, int, int, int);
-
-
void Display(int **, int , int );
-
// int *nrow_ptr = NULL, *ncol_ptr = NULL;
-
-
/* int *nrow_ptr, *ncol_ptr;
-
// Declare the nrow_ptr and ncol pointer
-
nrow_ptr = &nrow;
-
ncol_ptr = &ncol;
-
// Initialized the nrow_ptr and ncol_ptr
-
*/
-
-
-
int main(int argc, char *argv[])
-
{
-
-
int nMatrix;
-
int nrow, ncol, nrow_1, ncol_1;
-
// Number of row and column
-
-
int row_loop, col_loop;
-
// Loop for row and column
-
-
int **rowptr = NULL; // -- First Matrix
-
// A pointer to pointer to integer
-
-
int **rowptr_1 = NULL; // -- Second Matrix
-
int **result = NULL; // Result of two Matrix
-
int symmetric;
-
/*
-
Used while loop instead of dowhile because it's recommended
-
by Bjarne Stroustrup
-
*/
-
// ------------------------------------------------------------
-
while(1)
-
{ // Must allocate at least two matrix
-
printf("\nPlease Enter same dimension for two Matrix\n");
-
printf("\nHow many Matrix : ");
-
scanf("%d", &nMatrix);
-
switch(nMatrix)
-
{
-
case 1:
-
{
-
printf("How many Row : ");
-
scanf("%d", &nrow);
-
rowptr = (int **)malloc(sizeof(int **) * nrow);
-
if (rowptr == NULL)
-
{
-
perror("Dynamic Memory Allocation for row Fails");
-
}
-
printf("How many Column : ");
-
scanf("%d", &ncol);
-
// For each row, allocated ncol
-
for (row_loop=0;row_loop<nrow;row_loop++)
-
{
-
rowptr[row_loop] = (int *)malloc(sizeof(int *) * ncol);
-
if (rowptr[row_loop] == NULL)
-
{
-
perror("Dynamic Memory Allocation for column Fails");
-
}
-
}
-
-
for (row_loop=0;row_loop<nrow;row_loop++)
-
{
-
for (col_loop=0;col_loop<ncol;col_loop++)
-
{
-
printf("Enter the [%d][%d] number : ", row_loop, col_loop);
-
scanf("%d", &rowptr[row_loop][col_loop]);
-
}
-
}
-
break;
-
}
-
// ------------------------------------------------------------
-
case 2:
-
{ // Enter the first matrix
-
do
-
{
-
printf("Enter the First Matrix : \n");
-
printf("How many Row : ");
-
scanf("%d", &nrow);
-
rowptr = (int **)malloc(sizeof(int **) * nrow);
-
if (rowptr == NULL)
-
{
-
perror("Dynamic Memory Allocation for row Fails");
-
}
-
printf("How many Column : ");
-
scanf("%d", &ncol);
-
-
for (row_loop=0;row_loop<nrow;row_loop++)
-
{
-
rowptr[row_loop] =(int *) malloc(sizeof(int *) * ncol);
-
if (rowptr[row_loop] == NULL)
-
{
-
perror("Dynamic Memory Allocation for column Fails");
-
}
-
}
-
-
for (row_loop=0;row_loop<nrow;row_loop++)
-
{
-
for (col_loop=0;col_loop<ncol;col_loop++)
-
{
-
printf("Enter the [%d][%d] number : ", row_loop, col_loop);
-
scanf("%d", &rowptr[row_loop][col_loop]);
-
}
-
}
-
// -----------------------------------------------------------
-
// Enter the second Matrix
-
printf("Enter the Second Matrix : \n");
-
printf("How many row : ");
-
scanf("%d", &nrow_1);
-
rowptr_1 = (int **)malloc(sizeof(int **) * nrow_1);
-
if (rowptr_1 == NULL)
-
{
-
perror("Dynamic Memory Allocation Matrix 2 for row Fails");
-
}
-
printf("How many Column : ");
-
scanf("%d", &ncol_1);
-
-
for (row_loop=0;row_loop<ncol_1;row_loop++)
-
{
-
rowptr_1[row_loop] = (int *)malloc(sizeof(int *) * ncol_1);
-
if(rowptr_1[row_loop] == NULL)
-
{
-
perror("Dynamic Memory Allocation Matrix 2 for column Fails");
-
}
-
}
-
-
for (row_loop=0;row_loop<nrow_1;row_loop++)
-
{
-
for (col_loop=0;col_loop<ncol_1;col_loop++)
-
{
-
printf("Enter the [%d][%d] number : ", row_loop, col_loop);
-
scanf("%d", &rowptr_1[row_loop][col_loop]);
-
}
-
}
-
break;
-
}while(symmetric == 0);
-
}
-
}
-
// ------------------------------------------------------------
-
// Check the Dimension of Two Matrix
-
symmetric = Symmetric(nrow, ncol, nrow_1, ncol_1);
-
if (symmetric == 1)
-
{
-
printf("\nThe two Matrix is same dimension");
-
}
-
else
-
{
-
printf("\nThe two Matrix is different dimension");
-
}
-
-
// Runtime Error
-
Add(rowptr, rowptr_1, nrow, ncol, nrow_1, ncol_1);
-
// Adding the element of matrix
-
// Display(result, nrow, ncol);
-
// Display the result after successful allocated the memory
-
-
free(rowptr);
-
free(rowptr_1);
-
free(result);
-
}
-
-
-
return 0;
-
-
}
-
// -----------------------------------------------------------
-
int Symmetric(int nrow, int ncol, int nrow_1, int ncol_1)
-
{
-
int symmetric;
-
if (nrow == nrow_1 && ncol == ncol_1)
-
{
-
symmetric = 1;
-
return symmetric;
-
}
-
else
-
{
-
symmetric = 0;
-
return symmetric;
-
}
-
}
-
// ------------------------------------------------------------
-
void Add(int **rowptr, int **rowptr_1, int nrow,
-
int ncol, int nrow_1, int ncol_1)
-
{
-
int row_loop, col_loop;
-
int **result = NULL;
-
-
nrow = nrow_1;
-
ncol = ncol_1;
-
-
result= (int **)malloc(sizeof(int **) * nrow);
-
-
for (row_loop = 0; row_loop < nrow; row_loop++)
-
{
-
result[row_loop] = (int *)malloc(sizeof(int *) * ncol);
-
}
-
if (result == NULL)
-
{
-
perror("Dynamic Memory Allocation for result Fails");
-
}
-
-
for (row_loop = 0;row_loop < nrow; row_loop++)
-
{
-
for (col_loop = 0; col_loop < ncol; col_loop++)
-
{
-
// *(*(result + row_loop) + col_loop) = (*(*(rowptr + row_loop) + col_loop)) + (*(*(rowptr_1 + row_loop) + col_loop));
-
result[row_loop][col_loop]=rowptr[row_loop][col_loop]+rowptr_1[row_loop][col_loop];
-
}
-
}
-
Display(result,nrow,ncol);
-
-
}
-
-
// ------------------------------------------------------------
-
void Display(int **result, int nrow, int ncol)
-
{
-
int row_loop, col_loop;
-
// Loop for row and column
-
-
for (row_loop = 0; row_loop < nrow; row_loop++)
-
{
-
for (col_loop = 0; col_loop < ncol; col_loop++)
-
{
-
printf("The value of array at [%d][%d] is %d\n", row_loop, col_loop, *(*(result + row_loop) + col_loop) );
-
}
-
}
-
-
}
-
-
// ------------------------------------------------------------
-
-
-
Thanks
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Nils |
last post by:
Hi,
I want to create a dynamic array with pointer, without allocation of the
memory.
I tried it so:
objekt **ob= new objekt;
It is not working, because he will parameter for the Konstructor...
|
by: D |
last post by:
hi,
i would like to know how to calculate the size of a dynamic array
created using a dereference declaration like int *numbers and
allocating via malloc or calloc: numbers=(int...
|
by: chai |
last post by:
Can anyone help me in finding elements stored in a dynamic array in.
|
by: monkeydragon |
last post by:
#define MAX_TABLE = 1024;
BYTE* dynamic1D = new BYTE;
later..
i want to create a dynamic 2d ARRAY like this:
>]
>]
>]
|
by: romayankin |
last post by:
Hello All,
I'm writing cross-platform code so i'm bound to standards. Here is
the code I have:
~~~~~~~~~~~~~~~~~~
double **mx = new double*;
for(int i = 0; i < col - 1; i++) {
mx = new...
| |
by: Saul775 |
last post by:
Hello, all:
Just a general question that's been bothering me. Suppose I'd like to
create a 2D array of integers -- not using the vector template -- but I need
to dynamically create the array as...
|
by: vmohanaraj |
last post by:
Hi,
I have implemented a template class which encaptulates a 2d dynamic array. Basically the class looks like this:
template <class T>
class Dynamic2dArray {
private:...
|
by: mrtcnr |
last post by:
Hi everyone,
I am new at programming. I was googling for how to create an 2d dynamic array. But I couldnt find what i want.
I want to create an array which i will define during run time. For...
|
by: hn.ft.pris |
last post by:
I've got following code test C++'s functor. For the sake of
easy-reading, I omit some declearations.
#include <algorithm>
#include <functional>
using namespace std;
|
by: Opteron64 |
last post by:
Hi,
I'm trying to create and initialise a dynamic array within a nested structure. The structure is defined as followed:
(C++ code)
typedef unsigned char uchar;
typedef unsigned int uint;
...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |