#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.
Related
Programming in C for finding maximum in 2D array using dynamic memory allocation.
int main() {
int i, arr[m][n], j, m, n;
int max = 0;
int *ptr;
printf("enter the value m");
scanf("%d", m);
printf("enter the vaue of n");
scanf("%d", n);
ptr = (int*) malloc(m * n * 4);
printf("enter the values\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", ((ptr + i * n) + j));
}
}
max = arr[0][0];
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
if (max < *((ptr + i * n) + j));
max = *((ptr + i * n) + j);
}
}
printf("%d", max);
}
I made changes to your code to remove the error you are getting. I commented the changes that I have made.
int main()
{
/*int i, arr[][], j, m, n;*/
/* Because arr is allocated dynamically, you have to declare as a pointer to int. */
int i, *arr, j, m, n;
int max = 0;
int *ptr;
printf("enter the value m");
scanf("%d", m);
printf("enter the vaue of n");
scanf("%d", n);
/*ptr = (int*)malloc(m * n * 4);*/
/* It is better to use sizeof(int) because int does not have the same length of all computers. */
ptr = (int*)malloc(m * n * sizeof(int));
printf("enter the values\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", ((ptr + i * n) + j));
}
}
/*max = arr[0];*/
/* To get the first int at arr, you could also use arr[0], but not arr[0][0] because */
/* this would require that arr be an array of pointers to array of int's, which is not the case. */
max = *arr;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
if (max < *((ptr + i * n) + j));
max = *((ptr + i * n) + j);
}
}
printf("%d", max);
}
You must learn the algorithms and programming language C.
So you can find some courses in this site :
learn-c
try this code is functioned:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[]) {
int i, j, m, n;
int max;
int **ptr;
printf("enter the value m: ");
scanf("%d", &m);
printf("enter the vaue of n: ");
scanf("%d", &n);
ptr = (int **)malloc(n * sizeof(int *));
for (i = 0; i < m; i++) {
*(ptr + i) = (int *)malloc(m * sizeof(int));
for (j = 0; j < n; j++) {
scanf("%d", (*(ptr + i) + j));
}
}
max = **ptr;
printf("\n\nMatrix:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d ", *(*(ptr + i) + j));
if (max < *(*(ptr + i) + j))
max = *(*(ptr + i) + j);
}
printf("\n");
}
printf("\nthe max is %d \n", max);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
void printingArr(int** arr, int rows);
void sortingEachOneOfThem(int** pArr, int rows);
void sortingTheWholeArray(int** pArr, int rows);
void bubbleSort(int* arr);
void freeArray(int **a, int m);
int main(void)
{
int** pArr = 0;
int numOfRows = 0;
int sizes = 0;
printf("Enter number of rows: ");
scanf("%d", &numOfRows);
pArr = (int**) malloc(sizeof(int*) * numOfRows);
if (pArr == NULL)
{
printf("Unsuccessful malloc!\n");
return 1;
}
for (int i = 0; i < numOfRows; i++)
{
printf("Enter array length for row %d: ",i);
scanf("%d", &sizes);
pArr[i] = (int*) malloc(sizeof(int) * sizes + 1);
if (pArr[i] == NULL)
{
printf("Unsuccessful malloc!\n");
return 1;
}
pArr[i][0] = sizes;
for (int k = 1; k < sizes + 1; k++)
{
printf("Enter value for array: ");
scanf("%d", &pArr[i][k]);
}
}
printingArr(pArr, numOfRows);
sortingEachOneOfThem(pArr, numOfRows);
printingArr(pArr, numOfRows);
sortingTheWholeArray(pArr, numOfRows);
printingArr(pArr, numOfRows);
for (int i = 0; i < numOfRows; i++)
{
if (pArr[i] != NULL)
{
free(*(pArr + i));
}
}
//free(pArr);
system("pause");
return 0;
}
/*
this amazing, wonderfull piece of program prints the array given
input: int** arr, int rows
output: none
*/
void printingArr(int** arr, int rows)
{
int i = 0;
int k = 0;
for (i = 0; i < rows; i++)
{
for (k = 0; k <= arr[i][0]; k++)
{
printf("%d ", arr[i][k]);
}
printf("\n");
}
printf("\n");
}
/*
This beautiful function sorts the whole array, but its length of rows like a pyramid
input: int** arr, int rows
output: none
*/
void sortingTheWholeArray(int** pArr, int rows)
{
int* temp = 0;
int i = 0, k = 0;
for (i = 0; i < rows - 1; i++)
{
for (k = 0; k < rows - 1; k++)
{
if (pArr[k][0] > pArr[k + 1][0])
{
temp = pArr[k];
pArr[k] = pArr[k + 1];
pArr[k + 1] = temp;
}
}
}
}
/*
This little small function sorts every row of the array of arrays given to it
input: int** arr, int rows
output: none
*/
void sortingEachOneOfThem(int** pArr, int rows)
{
int i = 0;
for (i = 0; i < rows; i++)
{
bubbleSort(pArr[i]);
}
}
/*
This little piece of a code is a bubble sort, sorts the array given to it :)
input: int* arr, int rows
output: none
*/
void bubbleSort(int* arr)
{
int i = 1, k = 0;
for (i = 1; i < arr[0] - 1; i++)
{
for (k = 1; k <= arr[0] - i; k++)
{
if (arr[k] > arr[k + 1])
{
arr[k] += arr[k + 1];
arr[k + 1] = arr[k] - arr[k + 1];
arr[k] -= arr[k + 1];
}
}
}
}
the free at the end crashes my code, showing this error:
https://i.stack.imgur.com/nqxBG.png
the same usage of the function free() on another code worked good, but not here. I have tried running through it in step by step mode, it crashes at the first free. Dr. memory shows this: https://i.stack.imgur.com/rSZJr.png
another link: https:// i.stack. imgur.com/ZX2Ne.png (paste it without the spaces in the middle, Can't post more than 2 links)
what can I do?
This:
pArr[i] = (int*) malloc(sizeof(int) * sizes + 1);
under-allocates. Adding a single byte to the size of an array of int makes little sense. You probably meant:
pArr[i] = malloc((sizes + 1) * sizeof *pArri[i]);
Don't cast the return value of malloc(), and use sizeof on the left-hand side.
Well i m struggling with the pointers, why doesn t work the function biggest in the end (yes of that s dummy function)? (exit code 6)
Ty for help
code:
int search(int const a[], int n, int key) {
for (int *i = a; i < a + n; i++) {
if ( key == *i ) return 1;
}
return 0;
}
void print_row(int const a[], int n, int row) {
for (int *i = a + n * row; i < a + n * (row + 1); i++) {
printf("%d ", *i);
}
printf("\n");
}
void biggest(double x, long *int_part, double *frac_part) {
*int_part = (long) x;
*frac_part = x - *int_part;
}
main () {
int tempretures[7][24];
for (int *i = &tempretures[0][0]; i < &tempretures[7][24]; i++) {
static int j = 1;
*i = j;
j+=2;
}
tempretures[6][5] = 32;
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 24; j++) {
printf("%d ", tempretures[i][j]);
}
printf("\n");
}
printf("Is it: %d\n", search(tempretures, 7*24, 32));
for (int i = 0; i < 7; i++) {
print_row(tempretures, 24, i);
}
long a = 0; double b = 0;
biggest(5.67, &a, &b);
printf("%li", a);
}
for (int *i = &tempretures[0][0]; i < &tempretures[7][24]; i++) {
static int j = 1;
*i = j;
j+=2;
}
This runs way past the end of the array, stopping only when it hits every element of row number 7, but there is no row number 7.
I have an array, in which I need to find the number of row, which has the smallest sum of powers. Consider the example array:
1 1 1
2 2 2
3 3 3
The first row, (index = 0), the sum of powers is equal to 3 (1^2 + 1^2 + 1^2 = 3).
The second row, (index = 1), the sum of powers is equal to 12 (2^2 + 2^2 + 2^2 = 12).
The third row, (index = 2), the sum of powers is equal to 27 (3^2 + 3^2 + 3^2 = 27).
But my program shows the wrong result - instead of index 0, it shows index 2. What is the problem?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int ** alloc(int n, int m)
{
int **array, i;
array = malloc(sizeof(int*) * n);
for(i=0; i<n; i++)
array[i] = malloc(sizeof(int) * m);
return array;
}
void show(int **array, int n, int m)
{
int i,j;
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
printf("%d\t", array[i][j]);
}
printf("\n");
}
}
void fill(int **array, int n, int m, int x)
{
int i,j;
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
array[i][j] = x;
}
}
}
void dealloc(int **array, int n, int m)
{
int i;
for(i=0; i<n; i++)
free(array[i]);
free(array);
}
int smallest_powers_sum(int **array, int n, int m)
{
int sum = 0, minsum = 0, i, j, minindex = -1;
for(i=0; i<n; i++)
{
minsum = sum;
sum = 0;
for(j=0; j<m; j++)
{
sum += pow(array[i][j], 2);
}
if(minsum <= sum)
minindex = i;
printf("sum = %d, minsum = %d, minindex = %d\n", sum, minsum, minindex);
}
return minindex;
}
int main(int argc, char **argv)
{
int **array, n1 = 3, m1 = 3;
array = alloc(n1, m1);
fill(array, n1, m1, 1);
array[0][0] = 1;
array[0][1] = 1;
array[0][2] = 1;
array[1][0] = 2;
array[1][1] = 2;
array[1][2] = 2;
array[2][0] = 3;
array[2][1] = 3;
array[2][2] = 3;
int sum = smallest_powers_sum(array, n1, m1);
printf("index = %d\n", sum);
dealloc(array, n1, m1);
return 0;
}
I think you meant this:
if(sum <= minsum ){
minindex = i;
minsum = sum;
}
Also, initially assign integer_max to minsum.
Here's the code:
int smallest_powers_sum(int **array, int n, int m)
{
int sum = 0, minsum = 1000000, i, j, minindex = -1;
for(i=0; i<n; i++)
{
sum = 0;
for(j=0; j<m; j++)
{
sum += pow(array[i][j], 2);
}
if(sum <= minsum ){
minindex = i;
minsum = sum;
}
printf("sum = %d, minsum = %d, minindex = %d\n", sum, minsum, minindex);
}
return minindex;
}
This line kills all your "memory" of previous rows:
minsum = sum;
You should only update minsum together with updating minindex.
Also your condition for updating minindex seems to be the wrong way around.
This would be one way to do (although a bare-bones version):
#define NROWS 3
#define NCOLS 3
int *getminrow(int *p, int n)
{
int minval = *p;
int *minaddr = p;
while (n >= 1) {
if (*p < minval) {
minval = *p;
minaddr = p;
}
n--;
p++;
}
return minaddr;
}
int sumrow(int *p, int n)
{
int sum = 0;
while (n >= 1) {
sum += (*p);
n--;
p++;
}
return sum;
}
int main(void)
{
int *array = malloc(NROWS * NCOLS);
int listsum[NROWS];
int i, j;
array[0 * NROWS + 0] = 1;
array[0 * NROWS + 1] = 1;
array[0 * NROWS + 2] = 1;
array[1 * NROWS + 0] = 2;
array[1 * NROWS + 1] = 2;
array[1 * NROWS + 2] = 2;
array[2 * NROWS + 0] = 3;
array[2 * NROWS + 1] = 3;
array[2 * NROWS + 2] = 3;
for (i = 0; i < NROWS; i++) {
for (j = 0; j < NCOLS; j++) {
array[i * NROWS + j] = pow(array[i * NROWS + j], 2);
}
}
for (i = 0; i < NROWS; i++)
listsum[i] = sumrow(&array[i], NCOLS);
printf("Min row: %d\n", getminrow(listsum, NROWS) - listsum);
return 0;
}
I think there are some problem in your logic. First "if(minisum<=sum)". It must be "if(sum<=minisum)". Second, if you're just using coding like that, then the condition will be considered only condition i and i-1. So, my advice to change the syntax "if(minisum<=sum)" with the following script. (There are new variables that I call minisumtemp).
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int ** alloc(int n, int m)
{
int **array, i;
array = malloc(sizeof(int*) * n);
for(i=0; i<n; i++)
array[i] = malloc(sizeof(int) * m);
return array;
}
void show(int **array, int n, int m)
{
int i,j;
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
printf("%d\t", array[i][j]);
}
printf("\n");
}
}
void fill(int **array, int n, int m, int x)
{
int i,j;
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
array[i][j] = x;
}
}
}
void dealloc(int **array, int n, int m)
{
int i;
for(i=0; i<n; i++)
free(array[i]);
free(array);
}
int smallest_powers_sum(int **array, int n, int m)
{
//int sum = 0, minsum = 0, i, j, minindex = -1;
int sum = 0, minsum = 0, i, j, minindex = 0, minsumtemp; // add new variable : minsumtemp
for(i=0; i<n; i++)
{
minsum = sum;
sum = 0;
for(j=0; j<m; j++)
{
sum += pow(array[i][j], 2);
}
//if(minsum <= sum) this the problem
// minindex = i;
if (i==0) minsumtemp = sum; // initialized first value for minsumtep
if (sum<=minsumtemp)
{
minindex = i;
minsumtemp = sum;
}
printf("sum = %d, minsum = %d, minindex = %d\n", sum, minsum, minindex);
}
return minindex;
}
int main(int argc, char **argv)
{
int **array, n1 = 3, m1 = 3;
array = alloc(n1, m1);
fill(array, n1, m1, 1);
array[0][0] = 1;
array[0][1] = 1;
array[0][2] = 1;
array[1][0] = 2;
array[1][1] = 2;
array[1][2] = 2;
array[2][0] = 3;
array[2][1] = 3;
array[2][2] = 3;
int sum = smallest_powers_sum(array, n1, m1);
printf("index = %d\n", sum);
dealloc(array, n1, m1);
return 0;
}
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');
}