Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Write a complete program.
The program should read two integers: m and n, from the keyboard.
Allocate memory for a dynamic 2D m * n array of doubles.
Initialize the array according to the formula A[i][j]=(i-5)/(j+1).
My code:
#include <stdio.h>
#include <math.h>
int main() {
int m,n;
scanf ("%d %d",&m &n);
double**A=(double**) malloc (m*size of (double*));
double*B=(double*) malloc (m*n * size of (double));
for (int i=0;i<n;i++){
A[i]=B+i*m;
}
for (int i=0;i<n;i++){
for (int j=0;i<m;j++){
A[i][j]=(i-5)/(j+1);
}
}
free (A);
free (B);
return 0;
}
Few things wrong:
size not defined.
#include <math.h> is not needed since you don't call any math functions.
Strictly speaking you should #include <stdlib.h> as this declares malloc() and friends.
A , is missing in scanf ("%d %d",&m &n);.
No need to cast malloc() return value.
Always start variable names with a small letter.
You have one } too many.
Indentation and code formatting is really bad and is asking for trouble.**
...
Check this section of the C-FAQ to see if you've done the allocation correctly.
Idea: Read through the C-FAQ mentioned above, it will teach you a lot and will turn out to be a valuable time investment.
**Here's an example of good code indentation & formatting (without fixes):
int main()
{
int m;
int n;
scanf("%d %d", &m, &n);
double** A = malloc(m * size of (double*));
double* B = malloc(m * n * size of (double));
for (int i = 0; i < n; i++)
{
B[i] = B + i * m;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; i < m; j++)
{
A[i][j] = (i - 5) / (j + 1);
}
}
free(A);
free(B);
return 0;
}
Well, I fixed the superfluous }.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
The purpose of this exercise is to use the two-subscript method of dynamic memory allocation.
Input for this program is a two-dimensional array of floating point data located in a file named
testdata2. The input array will contain 3 rows of data with each row containing 5 columns of data.
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
int temp;
int number;
int r = 3;
int c = 5;
fp = fopen("testdata2.txt", "r");
number = (int)malloc(r * c * sizeof(int));
while (fscanf(fp, "%d", &temp) != EOF){
for(int i = 0; i < 3;i++){
for(int j = 0; j < 5; j++){
temp = number[i][j];
}
}
}
return(0);
}
Among the plethora of things incorrect in your code (any one of which can result in undefined behavior):
The core data type is wrong. The question specifically calls for floating-point values, yet you're using integer types.
The receiver of any memory allocation in C should be a pointer; you're using a simple int.
You're hiding whatever warnings/errors you're receiving by hard-casting. Casting malloc in C isn't necessary, nor advised.
Even if everything else were fixed, your assignment statement for temp = ... is backward. You want to save the value just-read into your matrix, not throw it away and overwrite it with whatever undefined value resides in your memory-just-allocated.
All of that said, knowing the width of your array of arrays is five, the problem reduces to this. Note temp isn't needed at all
#include <stdio.h>
#include <stdlib.h>
int main()
{
static const size_t r = 3;
static const size_t c = 5;
FILE *fp = NULL;
double (*number)[c] = NULL; // pointer to array of dimension c.
fp = fopen("testdata2.txt", "r");
if (fp == NULL)
{
perror("Failed to open file: ");
return EXIT_FAILURE;
}
number = malloc(r * sizeof *number); // allocate r-rows of dimension c
if (number == NULL)
{
perror("Failed to allocate array of arrays: ");
return EXIT_FAILURE;
}
for (size_t i=0; i<r; ++i)
{
for (size_t j=0; j<c; ++j)
{
if (fscanf(fp, "%lf", number[i]+j) != 1)
{
fprintf(stderr, "Failed to parse int at %zu,%zu", i, j);
return EXIT_FAILURE;
}
}
}
for (size_t i=0; i<r; ++i)
{
for (size_t j=0; j<c; ++j)
printf("%lf ", number[i][j]);
fputc('\n', stdout);
}
free(number);
return(0);
}
You are declaring an integer:
int number;
and you are allocating memory with malloc assuming it is a multi-dimensional array, then trying to access its elements in the same way.
Change the declaration to:
int **number;
it is not
(int)malloc(rcsizeof(int))
It is
(int*) malloc(rcsizeof(int))
One more mistake is that you can't access the elements as
temp=number[i][j];
Replace it with
temp=number[i*r+j]
Hope this helps
number = (int)malloc(r * c * sizeof(int));
In C, never cast the result of a malloc. If you had left out the cast to int, you'd have a diagnostic here telling you that number is not a pointer type.
You can do this:
int* number = malloc(r * c * sizeof(int));
But that gives you one big single dimensional array. You would need to dereference it like this:
temp = number[i * c + j];
If you want two dimensional indices as if you had declared it like this:
int number[r][c];
you need to allocate it in two stages:
int** number = malloc(r * sizeof(int*));
number[0] = malloc(r * c * sizeof(int));
for (int i = 1 ; i < r ; i++)
{
number[i] = &number[0][i * c];
}
That sets up a big array of ints and an intermediate array of pointers to ints for each row. Now you can do
temp = number[i][j];
Edit
Or you can do what Dmitri says which is this:
int (*number)[c] = malloc(r * c * sizeof(number[0][0]));
That effectively mallocs an array of r blocks of c ints in one go.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I try to write a fuction for inner product without using array subscripting. I have been looking the code for hours.
Still I could not fix mistake. Can someone fix it?
#include<stdio.h>
#include<stdlib.h>
int inner_product(int *a, int *b, int size)
{
int sum = 0, i;
for (i = 0; i < size; ++i)
{
printf("enter value for first array: ");
scanf("%d", &(a + i));
}
for (i = 0; i < size; ++i)
{
printf("enter value for first array: ");
scanf("%d", &(b + i));
}
for (i = 0; i < size; ++i)
sum += *(a + i) * *(b + i);
return sum;
}
int main()
{
int n, a, b;
printf("How many elements do you want to store? ");
scanf("%d", &n);
printf("%d", inner_product(&a, &b, n));
system("pause");
return 0;
}
You have so many errors in your code. First, you are not defining your arrays as arrays, but as single int values. You should define them correctly:
int *a, *b;
Second, you are not allocating your arrays. If you want to create them on runtime you will have to use malloc() after you get the size for them:
a = (int*)malloc(sizeof(int) * n);
b = (int*)malloc(sizeof(int) * n);
Third, it is not necessary to pass your array values as reference, because you will not need to modify them:
printf("%d", inner_product(a, b, n));
Inside your inner_product() function:
&(a + i) and &(b + i) won't give you the memory position of the array item (because it depends on the memory block size). You should use &a[i] and &b[i] instead to get the array values.
Because your variables a and b are arrays, your inner product should be calculated this way:
for (i = 0; i < size; ++i)
sum += a[i] * b[i];
Tip: be sure to freeing your allocated a and b arrays by calling free(a) and free(b) after calling inner_product().
That's it!
a and b must be arrays with memory allocated to them.
int a[1000];
int b[1000];
is a quick fix, but best if the code checks that inputs are no larger than 1000 (if this is only a class assignment, you can get away without such checks, but production code needs to be more robust). You would not need & before a and b when calling the function, i.e. instead call inner_product(a, b, n).
#include <stdio.h>
void spiral(int a[10][10]) {
printf("%d", a[1][3]);
}
int main() {
int r, c, j, i;
scanf("%d%d", &r, &c);
int a[r][c];
for (i = 0; i < r; i++)
for (j = 0; j < c; j++) {
scanf("%d", &a[i][j]);
}
spiral(a);
return 0;
}
When I give a 3 x 6 array
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
The output is 14, while it should be 10
How to fix this issue?
If you enter 3 and 6 for r and c (respectively) then the type of a is not int[10][10] (or int(*)[10] as the spiral argument really is), it's int[3][6]. The memory layout is completely different for the arrays, leading to undefined behavior.
You can solve this by passing the size along to the function, and using it in the declaration of the array:
void spiral(const size_t r, const size_t c, int (*a)[c]) { ... }
Call it like expected:
spiral(r, c, a);
As noted using int a[r][c] as argument might be easier to read and understand, but it gives a false impression that a is actually an array. It's not. The compiler treats the argument as a pointer to an array of c integers, i.e. int (*a)[c].
This makes me a little conflicted... On the one hand I'm all for making things easier to read and understand (which means it will be easier to maintain), on the other hand newbies often get it wrong and think that one can pass an array intact when in fact it decays to a pointer which can lead to misunderstandings.
A few things are wrong: in void spiral() you ask for a 2D-array of 10*10, but you do not give that. Keep that part as a variable, so only ask the type you receive and not what you want to receive and with creating a dynamic array you should always do that with malloc or calloc and free them afterwards. This might be a bit hard at first, but when you start creating bigger programs this is a must if you have a question or do not understand the pointers in the program called (*) then ask me:
#include <stdio.h>
#include <stdlib.h>
void spiral(int **a) {
printf("%d", a[1][3]);
}
int main() {
int r, c, j, i;
scanf("%d%d", &r, &c);
int **a = malloc(r * sizeof(int*));
for (i = 0; i < r; i++) {
a[i] = malloc(c * sizeof(int));
}
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
scanf("%d", &a[i][j]);
}
}
spiral(a);
for (i = 0; i < r; i++) {
free(a[i]);
}
free(a);
return 0;
}
Please bear with me as this is probably a very simple question but I am very new to C.
I am trying to malloc a specific array and then free it. However, the line:
M = malloc(N*sizeof(double *));
...doesn't work. Can somebody please explain to me why this is not working and what the solution would be? Many thanks in advance.
You should free all positions, not just the first. See this example I have wrote.
As mentioned from #Grijesh, the allocation is also wrong. The example covers allocation too. Moreover, I suggest you not to cast the return of malloc (more).
You have to think the 2D array, as a 1D array, where every cell of it is a pointer to a 1D array. A picture might help:
http://gsamaras.files.wordpress.com/2014/04/array2d-n.png
Here, 1D array that holds the pointers is to the left and every cell of it, points to another 1D array.
How many 1D arrays to the left? As many cells as you have in the left array.
Btw, Nelly I think this is not a silly question, it's something that gets beginners into trouble. ;)
EDIT:
About your new code, you had to have the same definition and declaration for matrix_free, as well as, call it as you should. What's definition, etc. ?? Answer.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define Nmax 9000
double **makeMatrix(int N);
void free_matrix(double **M,int N);
int main(void) {
int N;
for (N = 2; N < Nmax; N *= 2) {
double **L = makeMatrix(N);
printf("yes \n");
free_matrix(L, N);
printf("woo \n");
}
return 0;
}
double **makeMatrix(int N) {
int i, j;
double **M;
M = malloc(N * sizeof(double *));
for (i = 0; i < N; i++)
M[i] = malloc(N * sizeof(double));
for (i = 1; i < N; i++) {
for (j = 1; j < N; j++) {
M[i][j] = (i) * (j) * M_PI / N;
}
}
return (M);
}
void free_matrix(double **M, int N) {
int i;
for (i = 1; i <= N; i++) {
free(M[i]);
}
free(M);
}
And then I receive the youwho output. :) But, it will stop at a certain point, because NMAX is too big! Not only NMAX is too big, but N grows really fast ( N *=). Have you done the math in a piece of paper? Too big numbers. For example, if I do N +=, then, I can go until NMAX = 9000.
Debug tip:
How do I know in which loop it reaches?
I printed out the counter of the loop, like this
printf("woo %d\n",N);
Of course, if you feel sure for yourself, then I suggest you learning the debugger.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
The questions from my textbook is:
Write a function that returns a pointer to float and has two parameters: 1)a two dim array of floats with COL column and 2)an integer that represents the number of rows. Returned pointer should point to an array of floats containing the sum of the elements in the corresponding row of the 2 dim array.
My solution is:
float* ptr (float array[][COL], int rows) {
float *ptr;
int j;
for (j=0; j<COL; j++)
*ptr += array[rows][j];
return *ptr;
}
I just wonder whether this solution is correct? Thank you very much.
You've not allocated any memory for ptr to point at, so the answer is unavoidably wrong. You have to ensure that the memory will last long enough to be usable, so you can't return a pointer to an automatic array; you'll either have to have a static array (but how do you make it big enough), or you'll have to dynamically allocate the memory (malloc() et al) and then make sure the calling code frees what was allocated.
You also have algorithmic problems. You're accumulating all the values for all rows into a single value, whereas you're requested to calculate a separate value for each row.
Also, *ptr is a float; you'd need just return ptr; to have the types correct.
Your function name needs to be different, too.
#include <stdio.h>
#include <stdlib.h>
enum { COL = 7 };
float *row_sums(float array[][COL], int rows)
{
float *ptr = malloc(rows * sizeof(*ptr));
if (ptr != 0)
{
for (int i = 0; i < rows; i++)
for (int j = 0; j < COL; j++)
ptr[i] += array[i][j];
}
return ptr;
}
int main(void)
{
float data[][COL] = { { 1.0 }, { 2.0 }, { 0.0, 3.0 }, { -1.0, -2.0 } };
float *result = row_sums(data, 4);
for (int i = 0; i < 4; i++)
printf("%d: %.1f\n", i, result[i]);
free(result);
return(0);
}
The use of 4 is sub-optimal; it should be something like ROWS, where that's defined using:
enum { ROWS = sizeof(data) / sizeof(data[0]) } ;