I have a program that:
given a sequence of numbers,
sorts even numbers in ascending order, and
sort odd numbers in descending order, and
adds the sorted even numbers to an array followed by the sorted odd numbers.
Example:
Input: 1, 2, 3, 4, 5, 6
Expected output: 2, 4, 6, 5, 3, 1
Actual output: 1578032, 0, 3, 6, 6487408, 0
here is my code:
#include < stdio.h >
int main() {
input();
}
int input() {
int n;
printf("Enter The Number Of Elements You Want To Enter : ");
scanf("%d", & n);
int * ptr, i, ev = 0, od = 0;
ptr = (int * ) calloc(n, sizeof(int));
for (i = 0; i < n; i++) {
printf("Enter Number : ");
scanf("%d", (ptr + i));
if ( * (ptr + i) % 2 == 0) {
ev++;
} else {
od++;
}
}
sorteven( & ptr, ev, od, n);
}
int sorteven(int * ptr, int ev, int od, int n) {
int i, j = 0, swap = 0, * ptreven;
ptreven = (int * ) calloc(ev, sizeof(int));
for (i = 0; i < n; i++) {
if ( * (ptr + i) % 2 == 0) { *
(ptreven + j) = * (ptr + i);
j++;
}
}
for (i = 0; i < ev - 1; i++) {
for (j = 0; j < ev - i - 1; j++) {
if ( * (ptreven + j) > * (ptreven + j + 1)) {
swap = * (ptreven + j); *
(ptreven + j) = * (ptreven + j + 1); *
(ptreven + j + 1) = swap;
}
}
}
sortodd( & ptr, ev, od, n, & ptreven);
}
int sortodd(int * ptr, int ev, int od, int n, int ptreven) {
int i, k = 0, swap = 0, * ptrodd, j;
ptrodd = (int * ) calloc(od, sizeof(int));
for (i = 0; i < n; i++) {
if ( * (ptr + i) % 2 != 0) { *
(ptrodd + k) = * (ptr + i);
k++;
}
}
for (i = 0; i < od - 1; i++) {
for (j = 0; j < od - i - 1; j++) {
if ( * (ptrodd + j) < * (ptrodd + j + 1)) {
swap = * (ptrodd + j); *
(ptrodd + j) = * (ptrodd + j + 1); *
(ptrodd + j + 1) = swap;
}
}
}
merge( & ptr, ev, od, n, & ptreven, & ptrodd);
}
int merge(int * ptr, int ev, int od, int n, int * ptreven, int * ptrodd) {
int * ptrmerge, i, j;
ptrmerge = (int * ) calloc(n, sizeof(int));
for (i = 0; i < ev; i++) { *
(ptrmerge + i) = * (ptreven + i);
}
for (i = ev, j = 0; i < n; i++, j++) { *
(ptrmerge + i) = * (ptrodd + j);
}
display( & ptrmerge, n);
}
int display(int * ptrmerge, int n) {
int i;
printf("OUTPUT : ");
for (i = 0; i < n; i++) {
printf(" %d ", * (ptrmerge + i));
}
}
like this
#include <stdio.h>
#include <stdlib.h>
void input(void);
int main(void){
input();
}
void sorteven(int *ptr, int n);
void sortodd(int *ptr, int n);
void display(int *ptt, int n);
void input(void){
int n;
printf("Enter The Number Of Elements You Want To Enter : ");fflush(stdout);
scanf("%d", &n);
int *ptr, i, ev = 0, od = 0;
if((ptr = calloc(n, sizeof(int)))==NULL){
perror("malloc");
exit(EXIT_FAILURE);
}
for (i = 0; i < n; i++){
int v;
printf("Enter Number : ");fflush(stdout);
scanf("%d", &v);
if(v % 2 == 0){
ptr[ev++] = v;
} else {
ptr[n - ++od] = v;
}
}
sorteven(ptr, ev);
sortodd(ptr + ev, od);
display(ptr, n);
free(ptr);
}
void sorteven(int *ptr, int n){
//sort to ascending order
int i, j, temp;
for (i = 0; i < n - 1; i++){
for (j = 0; j < n - i - 1; j++){
if(ptr[j] > ptr[j + 1]){
int temp = ptr[j];
ptr[j] = ptr[j + 1];
ptr[j + 1] = temp;
}
}
}
}
void sortodd(int *ptr, int n){
//sort to descending order
int i, j, temp;
for (i = 0; i < n - 1; i++){
for (j = 0; j < n - i - 1; j++){
if(ptr[j] < ptr[j + 1]){
int temp = ptr[j];
ptr[j] = ptr[j + 1];
ptr[j + 1] = temp;
}
}
}
}
void display(int *ptr, int n){
int i;
printf("\nOUTPUT : ");
for (i = 0; i < n; i++){
if(i)
putchar(' ');
printf("%d", ptr[i]);
}
putchar('\n');
}
Related
I have a problem with the result of my m1 function when I check that some of the array between rank 0 and the last rank is empty and unfortunately none of the workarounds help to solve this problem.
Can anyone help me with this?
Where is the problem in this code?
And how can it be solved?
This is the code:
this is the code:
#include <stdlib.h>
#include <time.h>
#include <mpi.h>
#define N 1000
#define M 1000 / 2
int A[N][N], B[N][N], C[N][N];
int m1[M][M], m2[M][M], m3[M][M], m4[M][M], m5[M][M], m6[M][M], m7[M][M];
int A11[M][M], A12[M][M], A21[M][M], A22[M][M], B11[M][M], B12[M][M], B21[M][M], B22[M][M];
int C11[M][M], C12[M][M], C21[M][M], C22[M][M];
int rank, size, start_row, end_row;
void multiplym1(int mySize, int AA[M][M], int BB[M][M], int CC[M][M], int DD[M][M], int resfinal[M][M], int mystart_row, int myend_row)
{
int result1[mySize][mySize], result2[mySize][mySize];
for (int i = mystart_row; i < myend_row; i++)
{
for (int j = 0; j < mySize; j++)
{
result1[i][j] = AA[i][j] + BB[i][j];
result2[i][j] = CC[i][j] + DD[i][j];
}
}
for (int i = mystart_row; i < myend_row; i++)
{
for (int j = 0; j < mySize; j++)
{
resfinal[i][j] = 0;
for (int k = 0; k < mySize; k++)
{
resfinal[i][j] += (result1[i][k] * result2[k][j]);
}
}
}
}
int main(int argc, char const *argv[])
{
srand(time(NULL));
printf("\n------------------------* Initializing matrices *----------------------\n");
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
start_row = rank * (N / size);
if (rank + 1 == size)
{
end_row = N;
}
else
{
end_row = (rank + 1) * (N / size);
}
// printf("#%d: start is %d and end is %d\n", rank, start_row, end_row);
for (int i = start_row; i < end_row; i++)
{
for (int j = 0; j < N; j++)
{
A[i][j] = rand() % 50;
B[i][j] = rand() % 20;
C[i][j] = 0;
// printf("#%d: A[%d][%d] = %d\n", rank, i, j, A[i][j]);
}
}
// printf("#%d: Done\n\n", rank);
MPI_Barrier(MPI_COMM_WORLD);
// printf("#%d: start \n\n", rank);
start_row = 0;
end_row = 0;
start_row = rank * (M / size);
if (rank + 1 == size)
{
end_row = M;
}
else
{
end_row = (rank + 1) * (M / size);
}
// printf("#%d: start is %d and end is %d\n", rank, start_row, end_row);
for (int i = start_row; i < end_row; i++)
{
for (int j = 0; j < M; j++)
{
A11[i][j] = A[i][j];
A12[i][j] = A[i][j + M];
A21[i][j] = A[i + M][j];
A22[i][j] = A[i + M][j + M];
B11[i][j] = B[i][j];
B12[i][j] = B[i][j + M];
B21[i][j] = B[i + M][j];
B22[i][j] = B[i + M][j + M];
}
}
// printf("#%d: Done\n\n", rank);
MPI_Barrier(MPI_COMM_WORLD);
// printf("#%d: start For M1\n\n", rank);
start_row = 0;
end_row = 0;
start_row = rank * (M / size);
if (rank + 1 == size)
{
end_row = M;
}
else
{
end_row = (rank + 1) * (M / size);
}
printf("#%d: start is %d and end is %d\n", rank, start_row, end_row);
multiplym1(M, A11, A22, B11, B22, m1, start_row, end_row);
MPI_Barrier(MPI_COMM_WORLD);
int *counts = malloc(size * sizeof(int));
int *displs = malloc(size * sizeof(int));
for (int i = 0; i < size; i++)
{
counts[i] = (M / size) * M;
displs[i] = i * (M / size) * M;
}
counts[size - 1] = ((M / size) + (M % size)) * M;
MPI_Gatherv(&m1[start_row][0], counts[rank], MPI_INT, m1, counts, displs, MPI_INT, 0, MPI_COMM_WORLD);
printf("#%d: M1 DONE!!\n", rank);
if (rank == 0)
{
for (int i = 0; i < M; i += 49)
{
for (int j = 0; j < M; j += 100)
{
printf("#%d: m1[%d][%d] = %d\n", rank, i, j, m1[i][j]);
}
}
}
MPI_Finalize();
return 0;
}```
`
Download or See Image Here
The problem statement is that I need to perform dilation and erosion on a binary image and using a structure element of dimension given by the user typically a 1D or 2D array and it can be square matrix(n X n) or rectangle matrix (m X n) where m, n can be even or odd integers.
This is morphological image processing using C language.
I implemented the following code, but I am not getting output as expected. Can any one tell me my mistake?
I am taking origin as a // 2 and b // 2 of structuring element which is order of a X b.
The following is my main function in image processing.
#include "DEOC.h"
int main(int argc, char *argv[])<br>
{
FILE *fp, *op1, *op2, *op3, *op4;
fp = fopen(argv[1], "r");
op1 = fopen(argv[2], "wb");
op2 = fopen(argv[3], "wb");
op3 = fopen(argv[4], "wb");
op4 = fopen(argv[5], "wb");
if (fp == NULL)
{
printf("There is a problem in file opening!!! Please check again!!!");
exit(0);
}
int i, j, col, row, x, y, temp;
char ch1, ch2;
fscanf(fp, "%c%c", &ch1, &ch2);
fscanf(fp, "%d %d", &col, &row);
fprintf(op1, "%c%c\n", ch1, ch2);
fprintf(op1, "%d %d\n", col, row);
fprintf(op2, "%c%c\n", ch1, ch2);
fprintf(op2, "%d %d\n", col, row);
fprintf(op3, "%c%c\n", ch1, ch2);
fprintf(op3, "%d %d\n", col, row);
fprintf(op4, "%c%c\n", ch1, ch2);
fprintf(op4, "%d %d\n", col, row);
int *img, *dialated_img, *eroted_img, *opened, *closed, *structure;
img = (int *)malloc(row * col * sizeof(int));
dialated_img = (int *)malloc(row * col * sizeof(int));
eroted_img = (int *)malloc(row * col * sizeof(int));
opened = (int *)malloc(row * col * sizeof(int));
closed = (int *)malloc(row * col * sizeof(int));
printf("Enter x: - ");
scanf("%d", &x);
printf("Enter y: - ");
scanf("%d", &y);
structure = (int *)malloc(x * y * sizeof(int));
for (i = 0; i < x; i++)
{
for (j = 0; j < y; j++)
{
*(structure + i * y + j) = 1;
}
}
printf("Used Structure is: -\n");
for (i = 0; i < x; i++)
{
for (j = 0; j < y; j++)
{
printf("%d ", *(structure + i * y + j));
}
printf("\n");
}
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
fscanf(fp, "%d", &temp);
*(img + i * col + j) = temp;
*(dialated_img + i * col + j) = temp;
*(eroted_img + i * col + j) = temp;
*(opened + i * col + j) = temp;
*(closed + i * col + j) = temp;
}
}
dialation(img, dialated_img, structure, row, col, x, y);
erosion(img, eroted_img, structure, row, col, x, y);
opening(img, opened, structure, row, col, x, y);
closing(img, closed, structure, row, col, x, y);
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
fprintf(op1, "%d", *(dialated_img + i * col + j));
fprintf(op2, "%d", *(eroted_img + i * col + j));
fprintf(op3, "%d", *(opened + i * col + j));
fprintf(op4, "%d", *(closed + i * col + j));
}
fprintf(op1, "\n");
fprintf(op2, "\n");
fprintf(op3, "\n");
fprintf(op4, "\n");
}
fclose(fp);
fclose(op1);
fclose(op2);
fclose(op3);
fclose(op4);
return 0;
}
The DEOC.h file is
#include <stdio.h>
#include <stdlib.h>
void dialation(int *img, int *dialation_img, int *structure, int row, int col, int x, int y)
{
int s = x / 2;
int a = y / 2;
for (int i = s; i < row - s; i++)
{
for (int j = a; j < col - a; j++)
{
if (*(img + i * col + j) == 1)
{
continue;
}
else
{
for (int w = -1 * s; w <= s; w++)
{
for (int q = -1 * a; q <= a; q++)
{
if (*(structure + (w + s) * y + (q + a)) == 1)
{
if (*(img + (i + w) * col + (j + q)) == 1)
{
*(dialation_img + i * col + j) = 2;
continue;
}
}
}
}
}
}
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (*(dialation_img + i * col + j) == 2)
{
*(dialation_img + i * col + j) = 1;
}
}
}
}
void erosion(int *img, int *erosion_img, int *structure, int row, int col, int x, int y)
{
int s = x / 2;
int a = y / 2;
for (int i = s; i < row - s; i++)
{
for (int j = a; j < col - a; j++)
{
for (int w = -1 * s; w <= s; x++)
{
for (int q = -1 * a; q <= a; y++)
{
if (x == 0 && y == 0)
{
continue;
}
else
{
if (*(structure + (w + s) * y + (q + a)) == 1)
{
if (*(img + (i + w) * col + (j + q)) == 0)
{
*(erosion_img + i * col + j) = 2;
continue;
}
}
}
}
}
}
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (*(erosion_img + i * col + j) == 2)
{
*(erosion_img + i * col + j) = 0;
}
}
}
}
void opening(int *img, int *opened_img, int *structure, int row, int col, int x, int y)
{
erosion(img, opened_img, structure, row, col, x, y);
dialation(img, opened_img, structure, row, col, x, y);
}
void closing(int *img, int *closed_img, int *structure, int row, int col, int x, int y)
{
dialation(img, closed_img, structure, row, col, x, y);
erosion(img, closed_img, structure, row, col, x, y);
}
I am trying to use it but it is not executing properly.
Description of the problem :
Compute the number of all the sequences which go up down from some input n.
So the user input n; with that n then I create an array of numbers 1..n and then number the sequences with that property
Example: n = 4
1 3 2 4
1 4 2 3
2 3 1 4
2 4 1 3
3 4 1 2
Answer: 5
My program works but for some reason I sometimes get 0 instead of the answer.
#include <stdio.h>
#include <stdlib.h>
void *safeMalloc(int n) {
void *p = malloc(n);
if (p == NULL) {
printf("Error: malloc(%d) failed. Out of memory?\n", n);
exit(EXIT_FAILURE);
}
return p;
}
void swap(int *fir, int *sec) {
int temp = *fir;
*fir = *sec;
*sec = temp;
}
void permute(int *array, int i, int length, int *count) {
if (length == 2) {
*count = 1;
return;
}
if (length == i) {
int v = 0, flag = 1;
while (v < length) {
if (v % 2 == 0) {
if (array[v] < array[v + 1]) {
v++;
} else {
flag = 0;
return;
}
}
if (v % 2 != 0) {
if (array[v] > array[v + 1]) {
v++;
} else {
flag = 0;
return;
}
}
}
if (flag == 1) {
/*
int a;
for (a = 0; a < length; a++)
printf("%d", array[a]);
printf("\n");
*/
*count = *count + 1;
}
}
int j = i;
for (j = i; j < length; j++) {
swap(array + i, array + j);
permute(array, i + 1, length, count);
swap(array + i, array + j);
}
return;
}
int main(int argc, char **argv) {
int n;
scanf("%d", &n);
int *arr = safeMalloc(n * sizeof(int));
int i;
for (i = 0; i < n; i++) {
arr[i] = i + 1;
}
int count = 0;
permute(arr, 0, n, &count);
printf("%d\n", count);
return 0;
}
You basically generate all permutations of the array elements and count the valid ones.
Your code has a minor flaw:
the loop while (v < length) { goes one step too far: you access tab[v + 1] so the loop should stop at v < length - 1. As currently coded, it has undefined behavior.
You can further simply the code:
there should be no need to special case length == 2.
flag useless as you always return when you clear it.
if (v % 2 != 0) is redundant: else would suffice.
Here is a fixed and simplified version:
#include <stdio.h>
#include <stdlib.h>
void *safeMalloc(int n) {
void *p = malloc(n);
if (p == NULL) {
printf("Error: malloc(%d) failed. Out of memory?\n", n);
exit(EXIT_FAILURE);
}
return p;
}
void swap(int *fir, int *sec) {
int temp = *fir;
*fir = *sec;
*sec = temp;
}
void permutate(int *array, int i, int length, int *count) {
if (i == length) {
for (int v = 0; v < length - 1; v++) {
if (v % 2 == 0) {
if (array[v] >= array[v + 1]) {
return;
}
} else {
if (array[v] <= array[v + 1]) {
return;
}
}
}
*count = *count + 1;
} else {
for (int j = i; j < length; j++) {
swap(array + i, array + j);
permutate(array, i + 1, length, count);
swap(array + i, array + j);
}
}
}
int main(int argc, char **argv) {
int n;
if (scanf("%d", &n) == 1 && n > 0) {
int *arr = safeMalloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
arr[i] = i + 1;
}
int count = 0;
permutate(arr, 0, n, &count);
printf("%d\n", count);
}
return 0;
}
if you call tab(n,k) the number of updown sequence of length n with k being the last number in your sequence, you can write a recursive formula and implement it like that:
int N = 5+1;
int** tab = new int*[N];
for (int n = 0; n < N; n++) {
tab[n] = new int[N];
for (int k = 0; k < N; k++) {
tab[n][k] = 0;
}
}
tab[1][1] = 1;
for (int n = 2; n < N; n++) {
for (int k = 1; k <= n; k++) {
if (n % 2 == 0) {
for (int j = 0; j < k; j++) {
tab[n][k] += tab[n-1][j];
}
}
else {
for (int j = k; j < n; j++) {
tab[n][k] += tab[n-1][j];
}
}
}
}
int res = 0;
for (int j = 0; j < N; j++) {
res += tab[N - 1][j];
}
You can solve this without iterating through the permutations. Say you're trying to calculate f(n). Where can the new, high number go? It has to go in an 'up' position, which is an even position. You can have any valid sequence of odd length preceding it, and any valid sequence following it.
Let's say we're calculating f(n,k) where the highest val is in position k, zero indexed. This is zero for k even. For odd k we get:
f(n,k) = choose(n-1, k) * f(k) * f(n - k - 1)
To get f(n), sum f(n,k) over odd k < n.
We have to calculate the first few by hand.
f(0) = 1
f(1) = 1
f(2) = 1
f(3) = f(3,1) = choose(2,1) * f(1) * f(1) = 2 * 1 *1 = 2
f(4) = f(4,1) + f(4,3) = choose(3,1) * f(1) * f(2) + choose(3,3) * f(3) * f(0) = 3*1*1 + 1*2*1 = 5
f(5) = f(5,1) + f(5,3) = choose(4,1) * f(1) * f(3) + choose(4,3) * f(3) * f(1) = 4*1*2 + 4*2*1 = 16
I have a problem with increasing C[] array from 6 to 10 I've faced with heap corruption problem by realloc(). I have a following code of Big M metod:
#include <stdio.h>
#include <stdlib.h>
int * mnInit(FILE * );
double * * ReadA(FILE * , int, int);
double * ReadVector(FILE * , int);
int * condtxt(FILE *, int );
void Artif_counter(int* , int *, int);
void reallocationA(double**, int* , int , int);
void increaseA(int*, double**, int, int, int);
void increaseC(double *, int);
int main(int argc, char * argv[]) {
FILE * file1 = fopen("C.txt", "r");
FILE * file4 = fopen("A.txt", "r");
FILE * file5 = fopen("Agetmn.txt", "r");
FILE * file6 = fopen("cond.txt", "r");
int * ptr_mn;
ptr_mn = mnInit(file5);
int n = * (ptr_mn);
int m = * (ptr_mn + 1);
double * * A;
A = ReadA(file4, n, m);
double * C;
C = ReadVector(file1, n);
int * cond;
cond = condtxt(file6, m);
for(int i = 0; i < m; i++){
}
//--------------------------------------------------
int BAcounter = 0;
Artif_counter(cond, &BAcounter, m);
printf("\n Basys and Artifical variable = %d", BAcounter);
reallocationA(A, &n, m, BAcounter);
increaseA(cond, A, n, m, BAcounter);
this function dont't working
increaseC(C, n);
When I trying to print arrays: the A[][] was printed right while C[] array was printed by unknown numbers and after the program Mmetod.exe was closed with problem: A heap has been corrupted (parameters: 0x00007FFCA0C1F6B0).
// count of basys and artif
//------------------------------------------------After Adding a new columns:
printf("\n A[][] ARRAY:\n");
for (int i = 0; i < m; i++) {
printf("%d ", i);
for (int j = 0; j < n; j++) {
printf(" %.3f ", A[i][j]);
}
printf("\n");
}
printf("\n\tVECTOR C[]:\n");
for (int i = 0; i < n; i++) {
printf("%lf ", C[i]);
}
fclose(file1);
fclose(file4);
fclose(file5);
fclose(file6);
free(C);
for (int i = 0; i < m; i++) {
free(A[i]);
}
free(A);
printf("\n\n");
system("pause");
return 0;
}
int * mnInit(FILE * file) {
int c;
int digit = 0;
int column = 0;
int * mnArray = malloc(2 * sizeof( * mnArray));
if (file == NULL) perror("Warning!");
else {
while (!feof(file)) {
c = fgetc(file);
if (c == ';') {
column++;
}
if (c == ' ') {
digit++;
}
}
}
* (mnArray) = (digit / column) + 1; * (mnArray + 1) = column;
return mnArray;
}
double * * ReadA(FILE * file, int n, int m) {
double * * A = malloc(m * sizeof( * A));
for (int i = 0; i < m; i++) {
A[i] = malloc(n * sizeof( * A[i]));
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
//fscanf(file ,"%lf ", &(*(*(A + i) + j)));
fscanf(file, "%lf ", & A[i][j]);
}
fscanf(file, "\n");
}
return A;
}
double * ReadVector(FILE * file, int m) { // ++++
double * Vector = malloc(m * sizeof( * Vector));
for (int i = 0; i < m; i++) {
fscanf(file, "%lf ", Vector + i);
}
return Vector;
}
int* condtxt(FILE * file, int m){
int * condA = malloc(m * sizeof(*condA));
for(int i = 0; i < m; i++){
fscanf(file, "%d", &condA[i]);
}
return condA;
}
void Artif_counter(int * cond, int *k, int m){
for(int i = 0; i < m; i++){
if(cond[i] == -1){
(*k)++;
}
if(cond[i] == 0){
(*k)++;
}
if(cond[i] == 1){
*k = (*k) + 2;
}
}
}
void reallocationA(double** A, int* n, int m, int k){
double * tmp = NULL;
for(int i = 0; i < m; i++){
tmp = realloc(A[i], ((*n) + k)*sizeof(*A[i]));
if(tmp){
A[i] = tmp;
tmp = NULL;
} else{
printf("Error! Memory isn't reallocated'");
tmp = NULL;
}
}
(*n) = (*n) + k;
}
void increaseA(int* cond, double** A, int n, int m, int k){
int presentcol = n-k;
for(int i = 0; i < m; i++){
if(cond[i] == -1){
for(int j = 0; j < m; j++){
if(j == i){
A[j][presentcol] = 1;
} else {
A[j][presentcol] = 0;
}
}
presentcol++;
}
if(cond[i] == 0){
for(int j = 0; j < m; j++){
if(j == i){
A[j][presentcol] = 1;
} else {
A[j][presentcol] = 0;
}
}
presentcol++;
}
if(cond[i] == 1){
for(int j = 0; j < m; j++){
if(j == i){
A[j][presentcol] = 1;
A[j][presentcol + 1] = -1;
} else {
A[j][presentcol] = 0;
A[j][presentcol + 1] = 0;
}
}
presentcol = presentcol + 2;
}
}
}
When I wanted to increase an array in a simple code by this function it've done and I don't understand why(
A GNU debugger rewiev: 0 warning: Critical error detected c0000374
void increaseC(double * C, int n){
double * tmp;
tmp = realloc(C, (n)*sizeof(*C)); // found out that realloc function caused an error
if(!tmp){
printf("Error");
} else{
C = tmp;
}
tmp = NULL;
}
#include <stdio.h>
#include <stdlib.h>
float average_score(int** array_score)
{
int i, j;
int sum = 0;
for(i = 0; i < 4; i++)
{
for(j = 0; j < 4; j++)
{
sum += *(*(array_score + i) + j);
}
}
printf("sum / 16 = %d\n", sum / 16);
return (float) sum / 16;
}
int lowest_score(int** array_score)
{
int i, j;
int temp = **array_score;
for(i = 0; i < 4; i++)
{
for(j = 0; j < 4; j++)
{
if(temp > *(*(array_score + i) + j))
//------
temp = *(*(array_score + i) + j);
printf("%d ",*(*(array_score + i) + j));
}
//--
printf("\n");
}
printf("low_score = %d\n", temp);
return temp;
}
int main(int argc, char** argv)
{
int **array_score = NULL;
int i = 0;
int j = 0;
//Create a two-dimensional array
array_score = (int **) malloc(4 * sizeof(int *));
for(i = 0; i <= 4; i++)
array_score[i] = (int *)malloc(4 * sizeof(int));
//--
for(i = 0; i < 4; i++)
{
printf("Please enter the student_%d four grades, (separated with a space )\n", i+1);
int ret = scanf("%d %d %d %d", (*(array_score + i) + 0), (*(array_score + i) + 1),
(*(array_score + i) + 2), (*(array_score + i) + 3));
fflush(stdin);
if(4 != ret)
i--;
}
//There is something wrong with the function return value
float ave_score = average_score(array_score);
//--
int low_score = lowest_score(array_score);
//The output
printf("average score: %d\n lowest score: %d\n", ave_score, low_score);
return 0;
}
This allocates space for 4 pointers and then writes 5, since i takes on the values 0, 1, 2, 3 and 4:
array_score = (int **) malloc(4 * sizeof(int *));
for(i = 0; i <= 4; i++)
array_score[i] = (int *)malloc(4 * sizeof(int));
Note that in C you shouldn't cast the return from malloc. There's an answer on Stackoverflow telling all the details why this is not helpful.