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... - #include<stdio.h>
-
#include<stdlib.h>
-
-
-
void print2data(int *p, int rows[], int cols[], int start);
-
-
int main(void)
-
{
-
-
int cols, rows, start;
-
int *p;
-
-
printf("Enter starting value: ");
-
scanf("%d", &start);
-
printf("Enter row size: ");
-
scanf("%d", &rows);
-
printf("Enter # of rows: ");
-
scanf("%d", &cols);
-
-
-
p = (int*)malloc (rows * cols * sizeof(int));
-
-
print2data(p, rows, cols, start);
-
-
system("PAUSE");
-
}
-
-
-
void print2data(int *p, int rows, int cols, int start)
-
{
-
-
int i, j, end, last, colstart, rowend, colend, startrolend;
-
-
end = (rows*cols);
-
rows = end;
-
last = start + end;
-
rowend = rows-cols;
-
colstart = start + rowend;
-
colend = last - cols;
-
startrolend = start + start;
-
-
printf("rows*cols = %d\n", end);
-
printf("start number is %d\n", start);
-
-
printf("array is: \n");
-
for (i = start++; i < end; i++)
-
{
-
printf("%d ", i);
-
}
-
printf("\n");
-
for(j = i; j < last; j++)
-
{
-
-
printf("%dt ", j);
-
}
-
-
-
printf("\n");
-
-
}
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.
1 2073
[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:
That is an array of 5 elements each of which is an int.
won't compile. You need to declare the number of elements.
Second, this array:
is still an array of 5 elements. Each element is an array of 10 int.
is still an array of 5 elements. Each element is an array of 10 elements where each element is an array of 15 int.
won't compile. You need to declare the number of elements.
Third, the name of an array is the address of element 0
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*.
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: -
int array[5][10];
-
-
int (*ptr)[10] = array;
-
Fourth, when the number of elements is not known at compile time, you create the array dynamically: -
int* array = new int[value];
-
int (*ptr)[10] = new int[value][10];
-
int (*ptr)[10][15] = new int[value][10][15];
-
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: -
int** ptr = new int[value][10]; //ERROR
-
new returns the address of an array of 10 int and that isn't the same as an int**.
Likewise: -
int*** ptr = new int[value][10][15]; //ERROR
-
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: -
int array[10] = {0,1,2,3,4,5,6,7,8,9};
-
has a memory layout of
0 1 2 3 4 5 6 7 8 9
Wheras this array: -
int array[5][2] = {0,1,2,3,4,5,6,7,8,9};
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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;
}
|
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...
|
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...
|
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...
|
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/////////////////////...
|
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...
|
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...
|
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 )
...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
| |