I want my output print the array without any duplicate number
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define BUBBLE 5
int main()
{
int myArray[BUBBLE];
int i, j ,a , b, k;
int temp = 0;
int num;
int cunt, size;
cunt=0;
float floatType;
int integerType;
srand(time(NULL));
make the array randomlly
//Fill Array Randomlly
for (i = 0; i < BUBBLE; i ++)
{
num = rand() % BUBBLE + 1;
myArray[i] = num;
}
here my problem is not working
in the program tell me
Error value required as decrement operand
for (a = 0; i < BUBBLE; a++) {
for (b = a + 1; j < BUBBLE;) {
if (myArray[i] == myArray[i]) {
for (k = b; k < BUBBLE; k++) {
myArray[k] = myArray[k + 1];
}
BUBBLE --;
} else
b++;
}
}
Sort Array With Bobble Algorhim
here my sort
for(i = 0; i < BUBBLE; i++)
{
for (j = 0; j < BUBBLE-1; j++)
{
if (myArray[j] > myArray[j+1])
{
temp = myArray[j];
myArray[j] = myArray[j+1];
myArray[j+1] = temp;
cunt++;
}
}/*End inner for loop*/
}/*End outer for loop*/
the output
//Print Array After Sort
for (i = 0; i < BUBBLE; i++)
{
printf("%d\n",myArray[i]);
}
// Count For How Many Swap
printf("the numbeer of pases is %d \n" ,cunt);
printf("Size of float: %ld bytes\n",sizeof(floatType));
printf("Size of int: %ld bytes\n",sizeof(integerType));
system("PAUSE");
return 0;
}/*End of main*/
You may not change integer constants. At the compile-time this code snippet
for (a = 0; i < BUBBLE; a++) {
for (b = a + 1; j < BUBBLE;) {
if (myArray[i] == myArray[i]) {
for (k = b; k < BUBBLE; k++) {
myArray[k] = myArray[k + 1];
}
BUBBLE --;
} else
b++;
}
}
actually looks like
for (a = 0; i < 5; a++) {
for (b = a + 1; j < 5;) {
if (myArray[i] == myArray[i]) {
for (k = b; k < 5; k++) {
myArray[k] = myArray[k + 1];
}
5--;
^^^
} else
b++;
}
}
That is the compiler substitutes the name BUBBLE for the integer constant 5.
Moreover it is unclear where for example the variables i and j are initialized. And this condition in the if statement
if (myArray[i] == myArray[i]) {
^^^ ^^^
does not make sense.
You should declare a variable that will keep the actual number of elements in the array during deleting duplicates because you can not change the size of an already initialized array.
"Removing" duplicates can look as it is shown in the demonstrative program
#include <stdio.h>
#define BUBBLE 5
int main(void)
{
int a[BUBBLE] = { 1, 2, 1, 3, 2 };
for ( size_t i = 0; i < BUBBLE; i++ ) printf( "%d ", a[i] );
putchar( '\n' );
size_t n = 0;
for ( size_t i = 0; i < BUBBLE; i++ )
{
size_t j = 0;
while ( j < i && a[j] != a[i] ) ++j;
if ( j == i )
{
if ( n != i ) a[n] = a[i];
++n;
}
}
for ( size_t i = 0; i < n; i++ ) printf( "%d ", a[i] );
putchar( '\n' );
return 0;
}
Its output is
1 2 1 3 2
1 2 3
The variable n keeps the actual number of elements of the array after removing duplicates.
Related
Im trying to sort a matrix by the sum of its row's digits, from highest to lowest. I dont know if i explained that correctly so here's some photos explaining it.
This is what my code outputs. Basically, it asks you for m and n, which are the dimensions of the matrix. In this example it's a 3x4, 3 rows and 4 columns. Then, the matrix should be sorted by rows, by the sum of row's digits. Which means, instead of what's being outputted in the picture above, the correct result should be this:
I have no idea how to sort this from highest to lowest, i have been trying for hours to no avail.
Here's my code:
#include <stdio.h>
#define N 30
void main(){
double a[N][N], s[N], p;
int i, j, m, n, max;
while(1){
printf("\nm, n? ");
scanf("%d%d", &m, &n);
if(m <= 0 || m > N || n <=0 || n > N)
break;
for(i = 0; i < m; i++){
printf("%2d. row? ", i+1);
for(j = 0; j < n; scanf("%lf", &a[i][j++]));
}
for(i = 0; i < m; i++)
for(s[i] = j = 0; j < n; s[i] += a[i][j++]);
for(j = 0; j < n - 1; j++){
for(max = i, j = i+1; j < n; j++)
if(s[j] > s[max])
max = i;
if(max != j){
p = s[j];
s[j] = s[max];
s[max] = p;
for(j = 0; j < m; j++){
p = a[j][i];
a[j][i] = a[j][max];
a[j][max] = p;
}
}
}
printf("New matrix: \n");
for(i = 0; i < m; i++){
for(j = 0; j < n; printf("%8.2lf", a[i][j++]));
printf("\n");
}
for(j = 0; j < m; j++)
printf("-------------");
printf("\n");
for(j = 0; j < m; printf("%8.2f \n", s[j++]));
printf("\n");
}
}
You can sort the rows of the matrix from highest to lowest, using a simple bubble sort algorithm.Your code modified below:
int main() {
double a[N][N], s[N], p;
int i, j, m, n, max;
while (1) {
printf("\nm, n? ");
scanf("%d%d", & m, & n);
if (m <= 0 || m > N || n <= 0 || n > N)
break;
for (i = 0; i < m; i++) {
printf("%2d. row? ", i + 1);
for (j = 0; j < n; scanf("%lf", & a[i][j++]));
}
for (i = 0; i < m; i++)
for (s[i] = j = 0; j < n; s[i] += a[i][j++]);
for (i = 0; i < m - 1; i++) { // modified here
for (j = i + 1; j < m; j++) { // modified here
if (s[j] > s[i]) { // modified here
p = s[i];
s[i] = s[j];
s[j] = p;
for (int k = 0; k < n; k++) {
p = a[i][k];
a[i][k] = a[j][k];
a[j][k] = p;
}
}
}
}
printf("New matrix: \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; printf("%8.2lf", a[i][j++]));
printf("\n");
}
for (j = 0; j < m; j++)
printf("-------------");
printf("\n");
for (j = 0; j < m; printf("%8.2f \n", s[j++]));
printf("\n");
}
return 0;
}
Here's how i modified your code to achieve that:
Initialize a loop variable i to 0.
In the outer loop, run the inner loop j from i+1 to m-1.
In the inner loop, compare the sum of the row i with the sum of row
j. If the sum of row j is greater than the sum of row i, swap the
rows using a temporary variable.
After the inner loop finishes, increment the value of i by 1. Repeat
the outer loop until i becomes equal to m-1.
Output:
You can just use qsort to let it handle the sorting and item swapping. Then you only need to write the code for comparing two rows with each other.
Given something like this:
int matrix[3][4] =
{
{1,2,3,4},
{5,6,7,8},
{9,1,2,3},
};
You'd call qsort as:
qsort(matrix, 3, sizeof(int[4]), compare);
The only complexity is implementing the comparison callback function. There's two things to consider there:
We've told qsort that we have an array of 3 items, each of type int[4]. So the void pointers it passes along to us will actually be pointers to type int[4]. That is: int(*)[4].
qsort sorts in ascending order by default, where the item considered "less" ends up first. So we need to tweak that to get the largest item first.
Example:
int compare (const void* obj1, const void* obj2)
{
const int (*ptr1)[4] = obj1;
const int (*ptr2)[4] = obj2;
size_t sum1=0;
size_t sum2=0;
for(size_t i=0; i<4; i++)
{
sum1 += (*ptr1)[i];
sum2 += (*ptr2)[i];
}
if(sum1 > sum2) // largest sum considered "less" for qsort
return -1;
else
return 1;
return 0;
}
sum1 < sum2 would have placed the smallest row first.
Full example:
#include <stdio.h>
#include <stdlib.h>
int compare (const void* obj1, const void* obj2)
{
const int (*ptr1)[4] = obj1;
const int (*ptr2)[4] = obj2;
size_t sum1=0;
size_t sum2=0;
for(size_t i=0; i<4; i++)
{
sum1 += (*ptr1)[i];
sum2 += (*ptr2)[i];
}
if(sum1 > sum2) // largest sum considered "less" for qsort
return -1;
else
return 1;
return 0;
}
void print_matrix(size_t col, size_t row, int matrix[col][row])
{
for(size_t i=0; i<col; i++)
{
for(size_t j=0; j<row; j++)
{
printf("%d,", matrix[i][j]);
}
puts("");
}
}
int main (void)
{
int matrix[3][4] =
{
{1,2,3,4},
{5,6,7,8},
{9,1,2,3},
};
print_matrix(3,4,matrix);
puts("");
qsort(matrix, 3, sizeof(int[4]), compare);
print_matrix(3,4,matrix);
}
For example i have an array {1,2,2,2,3,3,3,3,4,4,4,4,4,}
I need to transform it into {4,4,4,4,4,,3,3,3,3,2,2,2,1}
So i have to sort it by the number of repetitions of an array element somehow.
I saw some solves of this problem on c++ but i have to write it on C.
I can`t use vector or smth. Just Sorting with buffer array or inside of exact array.
I tried something like this, but honestly now i etched into a dead end:
It is dont work properly as i ment.
I have an algoritm in my head:
Program count how times some element was repeated and write in into a second array
Program sort second array
Program sort first array by second array somehow
#include <stdio.h>
int main()
{
const int size = 10;
int A[size], B[size];
int counter1, counter2 = -1;
int temp = 0;
for (int i = 0; i < size; i++) {
printf("Enter %d element of array: ", i + 1);
scanf_s("%d", &A[i]);
}
for (int i = 0; i < size-1; i++) {
counter1 = 0;
counter2++;
for (int j = i; j < size; j++) {
if (A[j] == A[j - 1]) {
break;
}
if (A[j] == A[j+1]) {
counter1++;
B[counter2] = counter1;
temp = A[i];
}
}
}
for (int i = 0; i < size; i++) {
printf("El %d = %d\n",i+1,B[i]);
}
}
Not the best, but it does the job! I'm storing the final result in the Result[len][2]; variable! you can modify the code as you like
#include <stdio.h>
#include <string.h>
int main( )
{
int array[] = {1,2,2,2,3,3,3,3,4,4,4,4,4,11,10,6,6,6,6,6,6,6,6,6,6,6,6,};
int i,j,k=0,l,len,flag=0,temp0, temp1;
len = sizeof(array)/sizeof(array[0]);
int Result[len][2];
memset(Result,0,sizeof(Result));
for (i = 0 ;i < len ; i++ )
{
if (k != 0)
{
for (l= 0 ;l < k ; l++ )
if (array[i]== Result[l][0]) goto skip;
}
for(j= i ; j < len ; j++ )
{
if (array[i] == array[j])
{
Result[k][0] = array[j];
Result[k][1]++;flag = 1;
}
}
skip: if (flag == 1) {k++; flag = 0;}
}
for (i = 0; i < k; i++)
{
for(j= i+1 ; j < k ; j++ )
{
if(Result[i][1] < Result[j][1])
{
temp0 = Result[i][0];
temp1 = Result[i][1];
Result[i][0] = Result[j][0];
Result[i][1] = Result[j][1];
Result[j][0] = temp0 ;
Result[j][1] = temp1 ;
}
}
}
for (i = 0; i < k; i++) printf("[%d][%d]\n",Result[i][0],Result[i][1]);
return 0;
}
The output at my computer under the Linux environment is as follows:
[6][12]
[4][5]
[3][4]
[2][3]
[11][1]
[10][1]
[1][1]
how can I sort an array by the number of repetitions
Sort array A of size m by value .
Walk array and count number of different values: n
Allocate array B[] of size n of structs that has 2 members: value and occurrence.
Walk array A[] again and populate B[]
Sort B[] by .occurrence.
Walk B[] and re-populate A[].
O(m) memory, O(m *log m) time.
I want to store elements of maximum and minimum frequency in the arr2 array if there are more than one element of same frequency then both the elements should be stored ? But it is showing wrong results and i am not able to find what is the err. Can anyone help me with this. Any kind of help would be greatly appreciated.
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
int arr2[n];
int prevcount = 0;
int k = 0;
// for finding max element
for (int i = 0; i < n; i++)
{
int count = 0;
//counting the number of times it has occured
for (int j = 0; j < n; j++)
{
if (arr[i] == arr[j])
{
count++;
}
}
// checking if the same element was not there in the new array
for (int i = 0; i < k; i++)
{
if (arr[i] == arr[k])
{
goto nextit;
}
}
//it will update the kth element if the count is greater than the prev count
if (prevcount < count)
{
arr2[k] = arr[i];
}
//if these both are same but the number is different then will iterate k by 1 and store that element as well
else if (prevcount == count)
{
k++;
arr2[k] = arr[i];
}
prevcount = count;
nextit:
}
// for finding min element
prevcount = 1000;
for (int i = 0; i < n; i++)
{
int count = 0;
for (int j = 0; j < n; j++)
{
if (arr[i] == arr[j])
{
count++;
}
}
// checking if the same element was not there in the new array if there is then go to the next iteration
for (int i = 0; i < k; i++)
{
if (arr[i] == arr[k])
{
goto nextit2;
}
}
if (prevcount > count)
{
arr2[k] = arr[i];
}
else if (prevcount == count)
{
k++;
arr2[k] = arr[i];
}
prevcount = count;
nextit2:
}
for (int i = 0; i < k; i++)
{
printf("%d ", arr2[i]);
}
return 0;
}
As #SparKot suggests, sorting the array makes the problem simple. Would you please try:
#include <stdio.h>
#include <stdlib.h>
// compare values numerically
int numeric(const void *a, const void *b)
{
return (*(int *)a < *(int *)b) ? -1 : (*(int *)a > *(int *)b);
}
int main()
{
int n, i, j;
int *arr; // input array
int *count; // count frequency: initialized to 0's by calloc
int min = 0; // minimum occurrences
int max = 0; // maximum occurrences
scanf("%d", &n);
if (NULL == (arr = malloc(n * sizeof(int)))) {
perror("malloc");
exit(1);
}
if (NULL == (count = calloc(n, sizeof(int)))) {
perror("calloc");
exit(1);
}
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
qsort(arr, n, sizeof(int), numeric);
// count the length of sequence of the same numbers
for (i = 0; i < n; i++) {
for (j = 0; i + j < n && arr[i] == arr[i + j]; j++) {
;
}
count[i] = j; // i'th element has length j
i += j - 1; // jump to next number
}
// find minimum and maximum frequencies
for (i = 0; i < n; i++) {
if (count[i]) {
if (min == 0 || count[i] < min) min = count[i];
if (max == 0 || count[i] > max) max = count[i];
}
}
// report the result
for (i = 0; i < n; i++) {
if (count[i] == min) {
printf("min frequency %d value %d\n", count[i], arr[i]);
}
if (count[i] == max) {
printf("max frequency %d value %d\n", count[i], arr[i]);
}
}
return 0;
}
Sample input (n=10):
6
1
2
5
1
2
3
1
3
6
Output:
max frequency 3 value 1
min frequency 1 value 5
How do I get to write to 2D pointers where I have pnumber[2%4][2%4] and how can I get pnumber with more than 3 ciphers to be displayed?
I'm making a program to write pascals triangle in C.
When the pointer pnumbers[i][j] have both i and j = 2 mod 4, except for when i and j = 2, then my program won't write to the address and give the error message:
pascals triangle: malloc.c:2406: sysmalloc: Assertion '{old_top == initial_top (av) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
Aborted.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int factorial(int p) {
if (p>=1) {
return p*factorial(p-1);
}
else {
return 1;
}
}
int NchooseM(int n, int m) {
return factorial(n)/(factorial(n-m)*factorial(m));
}
int main() {
int n =7;
int x = n-2;
int i, j, k;
/*
printf("How many rows of Pascals triangle do you want to write?\n");
scanf("%d", &n);
*/
int **pnumbers;
pnumbers = (int **) malloc(n *sizeof(int *));
/* Allocate memory for storing the individual elements in a row */
for (i = 0; i < n; i++) {
pnumbers[i] = (int *) malloc(i * sizeof(int));
}
pnumbers[0][1] = 1;
/* Calculating the value of pnumbers[k][l] */
for (i = 0; i < n; i++) {
for (j = 0; j <= i; j++) {
pnumbers[i][j] = NchooseM(i,j);
}
/*
if (!(i % 4 == 2 && i != 2))
for (j = 0; j <= i; j++) {
pnumbers[i][j] = NchooseM(i,j);
} else if (i > 2) {
for (j = 0; j <= i-1; j++) {
pnumbers[i][j] = NchooseM(i,j);
}
}
*/
}
/* Writing out the triangle */
for (i = 0; i < n; i++) {
for (k = 0; k <= x; k++){
printf(" ");
}
for (j = 0; j <= i; j++) {
printf("%d ", pnumbers[i][j]);
}
x = x-1;
printf("\n");
}
for (i = 0; i < n; i++) {
free(pnumbers[i]);
}
free(pnumbers);
return 0;
}
When I avoid writing to these addresses and just print them out I get some seemingly random integer at these memory addresses.
Also when avoid these addresses and just print out so many rows that I get some spots with a higher integer with more than 3 siphers, it seems to overflow - and I don't see the logic behind it.
The result of running the second code
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int factorial(int p) {
if (p>=1) {
return p*factorial(p-1);
}
else {
return 1;
}
}
int NchooseM(int n, int m) {
return factorial(n)/(factorial(n-m)*factorial(m));
}
int main() {
int n =20;
int x = n-2;
int i, j, k;
/*
printf("How many rows of Pascals triangle do you want to write?\n");
scanf("%d", &n);
*/
int **pnumbers;
pnumbers = (int **) malloc(n *sizeof(int *));
/* Allocate memory for storing the individual elements in a row */
for (i = 0; i < n; i++) {
pnumbers[i] = (int *) malloc(i * sizeof(int));
}
pnumbers[0][1] = 1;
/* Calculating the value of pnumbers[k][l] */
for (i = 0; i < n; i++) {
/*
for (j = 0; j <= i; j++) {
pnumbers[i][j] = NchooseM(i,j);
}
*/
if (!(i % 4 == 2 && i != 2))
for (j = 0; j <= i; j++) {
pnumbers[i][j] = NchooseM(i,j);
} else if (i > 2) {
for (j = 0; j <= i-1; j++) {
pnumbers[i][j] = NchooseM(i,j);
}
}
}
/* Writing out the triangle */
for (i = 0; i < n; i++) {
for (k = 0; k <= x; k++){
printf(" ");
}
for (j = 0; j <= i; j++) {
printf("%d ", pnumbers[i][j]);
}
x = x-1;
printf("\n");
}
for (i = 0; i < n; i++) {
free(pnumbers[i]);
}
free(pnumbers);
return 0;
}
But row number 13 is still quite messed up.
Code is experiencing int overflow and thus undefined behavior (UB).
With 32-bit int and int factorial(int p), p > 12 oveflows the int range.
Code could use a wider integer type (long long works up to p==20), but improvements can be made at NchooseM() to avoid overflow for higher values.
Something like the below. Works up to int n = 30;
int NchooseM(int n, int m) {
// return factorial(n)/(factorial(n-m)*factorial(m));
int nm = 1;
int den = 1;
for (int i = m+1; i <= n; i++) {
assert(INT_MAX/i >= nm);
nm *= i;
assert(nm % den == 0);
nm /= den++;
}
return nm;
}
Tried unsigned long long and works up to int n = 62;
Edit: Another bug:
I "fixed" by initializing all to 1, yet I suspect something remains amiss in /* Calculating the value of pnumbers[k][l] */ for (i = 0; i < n; i++) { code.
pnumbers[i] = malloc((i + 1) * sizeof pnumbers[i][0]);
for (int j = 0; j < i + 1; j++) {
pnumbers[i][j] = 1;
}
Aside: rather than pnumbers[i] = (int *) malloc((i+1) * sizeof(int));, consider below with no unneeded cast nor trying to match the right type.
pnumbers[i] = malloc(sizeof pnumbers[i][0] * (i+1));
I am trying to write a C program to find if the sum of the elements in the middle three rows equals to sum of elements in the middle three columns of matrix print. So far am able to find the sum of all the columns and rows in the matrix.
#include<stdio.h>
void main()
{
static int array[10][10];
int i, j, m, n,sum = 0;
printf("Enter the order of the matrix\n");
scanf("%d %d", &m, &n);
if (m>=5&& n>=5)
{
printf("Enter the elements of the matrix\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
sum = sum + array[i][j] ;
}
printf("Sum of the %d row is = %d\n", i, sum);
sum = 0;
}
sum = 0;
for (j = 0; j < n; ++j)
{
for (i = 0; i < m; ++i)
{
sum = sum + array[i][j];
}
printf("Sum of the %d column is = %d\n", j, sum);
sum = 0;
}
}
else
{
printf("The matrix should be a 5 by 5 or bigger");
}
}
Try this
int mid1 = (m-3)/2;
int mid2 = (n-3)/2;
int sum1=0,sum2=0;
//suppose m is 9 index(0-8), so this for loop will add the index 3,4,5
for(i=mid1; i<mid1+3; i++)
{
for(j=0; j<n; j++)
{
sum1+=array[i][j];
}
}
for(i=0; i<m; i++)
{
for(j=mid2; j<mid2+3; j++)
{
sum2+=array[i][j];
}
}
if(sum1==sum2)
//equal
else
//not equal
Let's assume that the array has n rows and m columns. In this case you can calculate the sums of middle three rows and columns the following way
if ( n >= 3 && m >= 3 )
{
int k = ( n - 3 ) / 2;
int l = ( m - 3 ) / 2;
int cols_sum = 0;
int rows_sum = 0;
for ( i = k; i < k + 3; i++ )
{
for ( j = 0; j < m; j++ ) rows_sum += array[i][j];
}
for ( i = 0; i < n; i++ )
{
for ( j = l; j < l + 3; j++ ) cols_sum += array[i][j];
}
if ( rows_sum == cols_sum ) /* print appropriate message */;
}
Here is a demonstrative program
#include <stdio.h>
#define N 3
#define M 3
#define RANGE 3
int main( void )
{
int array[N][M] =
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
size_t n = N;
size_t m = N;
if ( n >= RANGE && m >= RANGE )
{
size_t k = ( n - RANGE ) / 2;
size_t l = ( m - RANGE ) / 2;
size_t i, j;
int cols_sum = 0;
int rows_sum = 0;
for ( i = k; i < k + RANGE; i++ )
{
for ( j = 0; j < m; j++ ) rows_sum += array[i][j];
}
for ( i = 0; i < n; i++ )
{
for ( j = l; j < l + RANGE; j++ ) cols_sum += array[i][j];
}
if ( rows_sum == cols_sum )
{
printf( "The sums of three middle rows and columns are equal "
"each other and have value %d\n", rows_sum );
}
else
{
printf( "The sums of three middle rows and columns are not equal "
"each other.\n"
"The sum of the rows has value %d "
"and the sum of the columns has value %d\n",
rows_sum, cols_sum );
}
}
return 0;
}
The output is
The sums of three middle rows and columns are equal each other and have value 45
Now all what you need is to provide the appropriate dimensions of the array and the user input of the data.
In you code you are asking user for row and column count, but you have already decided on that, moreover you cannot create a array dynamically with user input. However, there are ways you can work with to create arrays based on users request, we have Dynamic Memory Allocation concept to the rescue. For for time being, work with predefined size.
How to get row Data
In c, you have to fix on a row as i, in array[i][j] i is row and j is column reference, and loop through j to get all the row element values
How to get Column Data
same as above, loop through i and fix on j, exactly opposite.
#include<stdio.h>
int main()
{
/*This code works for only odd numbers and you have to make necessary changes to accommodate for even number of rows and columns*/
const int row = 5, colmn = 5;
static int array[row][colmn];
int i, j, m, n,sumRow = 0, sumColmn = 0;
int middleRow = row / 2, middleColmn = colmn / 2;
int howManyRows = 3;
printf("Enter the elements of the matrix\n");
/*
* first get elements from user
*/
for (i = 0; i < row; ++i)
{
for (j = 0; j < colmn; ++j)
{
printf("[%d][%d]",i,j);
scanf("%d", &array[i][j]);
}
}
/*
* printing elements to cross check the output
*/
for (i = 0; i < row; ++i)
{
for (j = 0; j < colmn; ++j)
{
printf("%d\t",array[i][j]);
}
printf("\n");
}
/*
* logic : get rows to adds (outer loop will loop through the rows to consider)
* inner loop is to get all the elements in each row
*/
for(int threeRows = 0 - howManyRows/2 ; threeRows <= howManyRows/2 ;threeRows++)
{
for (j = 0; j < row; ++j)
{
sumRow += array[j][middleColmn + threeRows];
}
}
/*
* its the same as above just the opposite
*/
for(int threeColmn = 0 - howManyRows/2; threeColmn <= howManyRows/2 ;threeColmn++)
{
for (j = 0; j < row ; ++j)
{
sumColmn += array[middleColmn + threeColmn][j];
}
}
printf("middleRow = %d middleColmn = %d sumRow = %d sumColmn = %d",middleRow, middleColmn,sumRow,sumColmn);
if(sumRow == sumColmn) printf("\nThey are equal");
else printf("\nnot equal");
return 0;
}