473,320 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

User defined 2D array problem

1
I have to create a program that outputs something like this:
Enter starting value: 50
Enter row size: 5
Enter # of rows: 7
50 51 52 53 54
55 56 57 58 59
60 61 62 63 64
65 66 67 68 69
70 71 72 73 74
75 76 77 78 79
80 81 82 83 84

So far I have something like this...
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4.  
  5. void print2data(int *p, int rows[], int cols[], int start);
  6.  
  7. int main(void)
  8. {
  9.  
  10.     int cols, rows, start;
  11.     int *p;
  12.  
  13.     printf("Enter starting value: ");
  14.     scanf("%d", &start);
  15.     printf("Enter row size: ");
  16.     scanf("%d", &rows);
  17.     printf("Enter # of rows: ");
  18.     scanf("%d", &cols);
  19.  
  20.  
  21.     p = (int*)malloc (rows * cols * sizeof(int));
  22.  
  23.     print2data(p, rows, cols, start);
  24.  
  25.     system("PAUSE");
  26. }
  27.  
  28.  
  29. void print2data(int *p, int rows, int cols, int start)
  30. {
  31.  
  32.     int i, j, end, last, colstart, rowend, colend, startrolend;
  33.  
  34.     end = (rows*cols);
  35.     rows = end;
  36.     last = start + end;
  37.     rowend = rows-cols;
  38.     colstart = start + rowend;
  39.     colend = last - cols;
  40.     startrolend = start + start;
  41.  
  42.     printf("rows*cols = %d\n", end);
  43.     printf("start number is %d\n", start);
  44.  
  45.     printf("array is: \n");
  46.     for (i = start++; i < end; i++)
  47.     {
  48.             printf("%d ", i);
  49.     }
  50.     printf("\n");
  51.         for(j = i; j < last; j++)
  52.         {
  53.  
  54.             printf("%dt ", j);
  55.         }
  56.  
  57.  
  58.         printf("\n");
  59.  
  60. }
But the output of the array keeps giving me the corret numbers, but theyare very misplaced. I know I have about 600 to many ints, and my for statements are jacked, but that were i'm getting confused. Any help at all would be great. Thanks.
Dec 15 '07 #1
1 2073
weaknessforcats
9,208 Expert Mod 8TB
[qupte=turtal]
p = (int*)malloc (rows * cols * sizeof(int));
[/quote]

This allocates a one-dimensional array. Both C and C++ only have one-dimensional arrays.

So why not run your loop from 0 to rows*cols and each time the loop counter % 5 is 0, output a newline??

Read this:
First, there are only one-dimensional arrays in C or C++. The number of elements in put between brackets:
Expand|Select|Wrap|Line Numbers
  1. int array[5];
  2.  
That is an array of 5 elements each of which is an int.

Expand|Select|Wrap|Line Numbers
  1. int array[];
  2.  
won't compile. You need to declare the number of elements.

Second, this array:
Expand|Select|Wrap|Line Numbers
  1. int array[5][10];
  2.  
is still an array of 5 elements. Each element is an array of 10 int.

Expand|Select|Wrap|Line Numbers
  1. int array[5][10][15];
  2.  
is still an array of 5 elements. Each element is an array of 10 elements where each element is an array of 15 int.


Expand|Select|Wrap|Line Numbers
  1. int array[][10];
  2.  
won't compile. You need to declare the number of elements.

Third, the name of an array is the address of element 0
Expand|Select|Wrap|Line Numbers
  1. int array[5];
  2.  
Here array is the address of array[0]. Since array[0] is an int, array is the address of an int. You can assign the name array to an int*.

Expand|Select|Wrap|Line Numbers
  1. int array[5][10];
  2.  
Here array is the address of array[0]. Since array[0] is an array of 10 int, array is the address of an array of 10 int. You can assign the name array to a pointer to an array of 10 int:
Expand|Select|Wrap|Line Numbers
  1. int array[5][10];
  2.  
  3. int (*ptr)[10] = array;
  4.  
Fourth, when the number of elements is not known at compile time, you create the array dynamically:

Expand|Select|Wrap|Line Numbers
  1. int* array = new int[value];
  2. int (*ptr)[10] = new int[value][10];
  3. int (*ptr)[10][15] = new int[value][10][15];
  4.  
In each case value is the number of elements. Any other brackets only describe the elements.

Using an int** for an array of arrays is incorrect and produces wrong answers using pointer arithmetic. The compiler knows this so it won't compile this code:

Expand|Select|Wrap|Line Numbers
  1. int** ptr = new int[value][10];    //ERROR
  2.  
new returns the address of an array of 10 int and that isn't the same as an int**.

Likewise:
Expand|Select|Wrap|Line Numbers
  1. int*** ptr = new int[value][10][15];    //ERROR
  2.  
new returns the address of an array of 10 elements where each element is an array of 15 int and that isn't the same as an int***.

With the above in mind this array:
Expand|Select|Wrap|Line Numbers
  1. int array[10] = {0,1,2,3,4,5,6,7,8,9};
  2.  
has a memory layout of

0 1 2 3 4 5 6 7 8 9

Wheras this array:
Expand|Select|Wrap|Line Numbers
  1. int array[5][2] = {0,1,2,3,4,5,6,7,8,9};
  2.  
has a memory layout of

0 1 2 3 4 5 6 7 8 9

Kinda the same, right?

So if your disc file contains

0 1 2 3 4 5 6 7 8 9

Does it make a difference wheher you read into a one-dimensional array or a two-dimensional array? No.

Therefore, when you do your read use the address of array[0][0] and read as though you have a
one-dimensional array and the values will be in the correct locations.
Dec 16 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Simon Morgan | last post by:
Can somebody please explain why the following piece of code generates an error unless I change q to *q: typedef int Queue; int top = 9; void insert(Queue *q, int n) { q = n; }
17
by: Steve R. Hastings | last post by:
I have been studying Python recently, and I read a comment on one web page that said something like "the people using Python for heavy math really wish they could define their own operators". The...
1
by: Macca | last post by:
Hi, I want to have an array/arraylist of a user defined class. This class just holds a number of queue collections. For efficiency I want my arraylist/array to be type safe to avoid...
1
by: The Man with no name | last post by:
Hi, Can we create an array of user defined dimensions in the following way:? 1 Create a class( say CONSTANT) whose lone datamember is an integer which is called say num. 2 In the public section...
7
by: anupamsps | last post by:
HI all, let me explain the problem: In my simulation I am using two class: particle and Container. //////////// the outline of particle class/////////////////////...
5
by: no1zson | last post by:
I have been reading through many of the array questions and cannot find one that addresses my issue. Maybe someone can help me out. Same story, I am learning Java and have just written a CD...
12
by: Daniel Klein | last post by:
I'm pretty new at php and web stuff so please be gentle with me. I'm trying to get a form to submit when the user presses the Enter key. I do not want to use javascript. I've googled this to...
0
by: mohammadiz | last post by:
Hi how can I pass a user defined type or array of user defined type to a function? type: CREATE TYPE CARDDATARECTYPE AS (DATANAME VARCHAR(256), DATAVALUE VARCHAR(2048), ISDELETABLE SMALLINT ) ...
1
by: rolan | last post by:
I am trying to figure out how to store and access an array, which is created inside a function, and needs to be accessed outside the function. My user defined class has set and get functions for 2...
1
by: remya1000 | last post by:
i'm using VB.net 2003 application program. i'm trying to convert a VB6 program to VB.NET. The VB6 code i'm trying to convert is shown below. declared g_Share() array in module and trying to add...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.