#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define N_MAX 10
#define N_MIN 2
#define TEMPSIZE 1024
float RowSum(float **matrix, int sizearray,int row) {
float sum;
for (int j=0 ; j <= row; j++) {
sum = 0;
for (int i=0 ; i < sizearray; i++) {
sum = sum + matrix[j][i];
}
}
return sum;
}
float ColSum(float **matrix, int sizearray, int col) {
float sum;
for (int j = 0; j <= col; j++) {
sum = 0;
for (int i = 0; i < sizearray; i++) {
sum = sum + matrix[i][j];
}
}
return sum;
}
int RepNum(int arraysize, float **matrix) {
int i, j, counter = 0;
int temparray[N_MAX*N_MAX];
for (i = 0; i < N_MAX*N_MAX; i++) {
temparray[i] = 0;
}
for (i = 0; i < arraysize; i++) {
for (j = 0; j < arraysize; j++) {
temparray[(int)matrix[i][j]]++;
}
}
for (i = 1; i < arraysize*arraysize; i++) {
if (temparray[i] > 1)
counter++;
}
return counter;
}
void PrintArray(float **matrix, int arraysize) {
for (int i = 0; i<arraysize; i++)
{
for (int j = 0; j<arraysize; j++)
printf("%3d ", (int)matrix[i][j]);
printf("\n");
}
}
int CheckInt(float **matrix, int arraysize) {
int counter = 0;
for (int i = 0; i < arraysize; i++) {
for (int j = 0; j < arraysize; j++) {
if (((int)matrix[i][j]) != matrix[i][j])
counter++;
}
}
return counter;
}
void main() {
printf("Hello! this program will help you to find out if you have a magic square!\nPlease enter your matrix order and following it the numbers you'd like to check: \n");
float **matrix;
char input[TEMPSIZE];
int sizear = 0;
float correctsum = 0.0;
int counter = 0, row, column;
fgets(input, TEMPSIZE, stdin);
char* str = strstr(input, " ");
if (str == 0)
return;
str[0] = 0;
str++;
sizear = atof(input);
if (sizear > N_MAX || sizear < N_MIN)
{
printf("ERROR: The matrix size cannot be %d size. \n", sizear);
exit(0);
}
matrix = (float**)calloc(1, sizear * sizeof(float*));
for (column = 0; column < sizear; column++)
{
matrix[column] = (float*)calloc(1, sizear * sizeof(float));
}
for (row = 0; row < sizear; row++)
{
for (column = 0; column < sizear; column++)
{
char* temp = strstr(str, " ");
if (temp == 0) /*gets the last number*/
{
++counter;
matrix[row][column] = atof(str);
goto end;
}
if (atof(temp) <= sizear*sizear && atof(temp) > 0) { /*puts all numbers in matrix*/
temp[0] = 0;
matrix[row][column] = atof(str);
str = temp + 1;
++counter;
}
else {
printf("you cannot enter the number %.3f \n", atof(temp));
exit(0);
}
}
}
end:
if (counter > sizear*sizear) {
printf("you've entered %d numbers, while you should've enter %d numners \n", counter, sizear*sizear);
}
else if (counter < sizear*sizear) {
printf("you've entered %d numbers, while you should've enter %d numners \n", counter, sizear*sizear);
}
else if (counter == sizear*sizear) {
correctsum = (float)((sizear*(sizear*sizear + 1)) / 2);
float row = RowSum(matrix, sizear, 0);
float coul = ColSum(matrix, sizear, 0);
if (row == coul && row== correctsum && coul==correctsum && RepNum(sizear, matrix) ==0 && CheckInt(matrix,sizear)==0) {
printf("It's a magic matrix!\n");
PrintArray(matrix, sizear);
}
else {
printf("Not a magic square:\n");
if (row != coul && row != correctsum && coul != correctsum) {
printf("* Coloums and rows sum do not match.\n");
}
if (RepNum(sizear, matrix) != 0) {
printf("* There are repeating numbers.\n");
}
if (CheckInt(matrix, sizear) != 0) {
printf("* One of the numbers or more you've entered isn't integer.\n");
}
}
}
for (column = 0; column < sizear; column++)
{
free(matrix[column]);
}
free(matrix);
}
When I hit more than sizearrayXsizearray numbers it doesn't add into the counter and stops at sizearrayXsizearray counting. Can anyone point out why it doesn't count it?
The exercise asked to check if the input is a magic square (sum rows=columns=diagonals), making sure there are no duplicates, only int numbers and that I don't enter more or less than sizearrayXsizearray numbers. The input from the user should look like 3 1 2 3 4 5 6 7 8 9 where the first number (here it is 3) is the array size and the rest are the numbers that would be checked as a magic square.
When I hit more than sizearrayXsizearray numbers it doesn't add into the counter and stops at sizearrayXsizearray counting. Can anyone point out why it doesn't count it?
Sure, it's simple: the counter is incremented in the loops
for (row = 0; row < sizear; row++)
{
for (column = 0; column < sizear; column++)
{
…
}
}
- the inner statements are run through (at most) sizear × sizear times, so the counter, starting from 0, cannot reach a higher value.
Related
#include<stdio.h>
int a, b;
float arr[10][10];
void iden(float x[a][b]);
void diag(float x[a][b]);
void pri_sec(float x[a][b]);
int main() {
int i, j, n;
arr[a][b];
for (i = 0;; i++) {
printf("Enter the no. of rows and columns:\n");
scanf("%d %d", &a, &b);
if (a == b) {
printf("Enter the values in an array:\n");
for (i = 0; i < a; i++) {
for (j = 0; j < b; j++)
scanf("%f", &(arr[i][j]));
}
printf("Enter 1 to check it is an identity matrix or not, 2 to check it is diagonal matrix or not\
,3 to check principal and secondary diagonal are equal or not and any other no. to exit:\n");
scanf("%d", &n);
if (n == 1)
iden(arr);
if (n == 2)
diag(arr);
if (n == 3)
pri_sec(arr);
else
return 0;
} else {
printf("It is not a square matrix:\n");
return 0;
}
}
}
void iden(float x[a][b]) {
int i, j, flag = 1;
for (i = 0; i < a; i++) {
for (j = 0; j < b; j++) {
if (x[i][j] != 1 && x[j][i] != 0)
flag = 0;
}
}
if (flag == 1)
printf("It is an identity matrix:\n");
else
printf("It is not an identity matrix:\n");
}
void diag(float x[a][b]) {
int i, j, flag = 0;
for (i = 0; i < a; i++) {
for (j = 0; j < a; j++) {
if ((i == j && x[i][j] == 0) || (i != j && x[i][j] != 0)) {
flag = 1;
break;
}
}
}
if (flag == 0)
printf("It is a diagonal matrix:\n");
else
printf("It is not a diagonal matrix:\n");
}
void pri_sec(float x[a][b]) {
int i, k, flag = 0;
for (i = 0; i < a; i++) {
k = a - 1 - i;
if (x[i][i] != x[i][k]) {
flag = 1;
}
}
if (flag == 1)
printf("The principal and secondary diagonals are not equal:\n");
else
printf("The principal and secondary diagonals are equal:\n");
}
When I changed the indices as 10 10 for the functions it worked but for the other case it didn't.
#include <stdio.h>
int x[10][10];
int ROWS, COLS;
void func_vla(int array[10][10]) {
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
array[i][j] = i * j;
}
}
}
int main() {
scanf("%d %d", &ROWS, &COLS);
int x[ROWS][COLS], i, j;
func_vla(x);
for (i = 0; i < ROWS; i++)
for (j = 0; j < COLS; j++)
printf("%d\n", x[i][j]);
}
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
I have a problem. I need to write a program that checks if every row has the same sum, and if every column has same sum.
Example:
3 3 3
3 3 3
In this example output should be "YES" for rows and "YES" for columns.
Another example:
4 5 6
6 4 5
In this example, output should be "YES" for rows because sum of 1st and 2nd row is the same (15), and it should be "NO" for columns because sum is not the same (10,9,11).
I have made code that checks first row and first column than it compares if they are same as the other ones. I have made it to check if its not but I don't know how to check it if it is, I mean how to output "YES" for both cases.
This is my code:
#include <stdio.h>
int main() {
int mat[100][100];
int n, m;
int i, j;
int sumak = 0;
int sumar = 0;
int sumarp = 0;
int sumakp = 0;
do {
printf("Unesite brojeve M i N: ");
scanf("%d %d", &m, &n);
} while (m < 0 || m > 100 || n < 0 || n > 100);
printf("Unesite clanove matricee: ");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &mat[i][j]);
}
}
// suma of first row
for (i = 0; i < 1; i++) {
for (j = 0; j < n; j++) {
sumarp = sumarp + mat[i][j];
}
sumarp = sumarp + mat[i][j];
}
// sum of all rows
for (i = 1; i < m; i++) {
for (j = 0; j < n; j++) {
sumar = sumar + mat[i][j];
}
if (sumarp != sumar) {
printf("NE");
} else {
sumar = 0;
continue;
}
}
// sum of the first column
for (j = 0; j<1; j++) {
for (i = 0; i< m;i++ ) {
sumakp = sumakp + mat[i][j];
}
sumakp = sumakp + mat[i][j];
}
// sum of every column
for (j = 1; j < n; j++) {
for (i= 0; i < m; i++) {
sumak = sumak + mat[i][j];
}
if (sumakp == sumak) {
sumak=0;
continue;
}
if(sumakp!=sumak)
{
printf("NE");
return 0;
}
else{
printf("DA");
}
}
}
So if someone can explain me how to do the rest of it.
Thank you!
Use a variable to indicate whether any of the rows or columns didn't match. Then check it at the end of the loop.
Here's how to do it for rows. Columns are similar, just switch the order of the i and j loops.
int all_matched = 1;
for (int i = 0; i < m; i++) {
int sumar = 0;
for (int j = 0; j < n; j++) {
sumar += mat[i][j];
}
if (sumar != sumarp) {
all_matched = 0;
break;
}
}
if (all_matched) {
printf("EQ\n");
} else {
printf("NE\n");
}
I wrote a code that tries to multiply two matrices and put them in another result matrix. The code is working (I think) but it prints a very strange output .. I think it has to do with one of the functions pointing to something diffrent than the values, not sure though.
I tried checking each function in separete, also inizialzing each matrix with {0}.
#include <stdio.h>
#define SIZE 5
//#pragma warning(disable:4996)
//#define _CRT_SECURE_NO_WARNINGS
void read_mat(int mat[][SIZE])
{
int i, j, k = 0;
char s[100]; // assign input str to 's'
fgets(s,25,stdin); // recieving only the first 25 numbers
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
if (s[k] == '\0') { //if the string is just \0 -- the end of the string
mat[i][j] = 0;
}
else { // is there are values in s
if (s[k] != '0') { // binary matrix -- only 0 or 1
mat[i][j] = 1;
k++;
}
else {
mat[i][j] = 0;
++k;
}
}
}
}
}
void mult_mat(int mat_a[][SIZE], int mat_b[][SIZE], int result_mat[][SIZE])
{
int i, j, k;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
for (k = 0; k < SIZE; k++) {
result_mat[i][j] += mat_a[i][k] * mat_b[k][j]; // by definition of matrix multiplication
k++;
}
}
}
}
void print_mat(int mat[][SIZE])
{
int i, j, k = 0;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
printf("%3d", &(mat[i][j])); // printing each value with a 3*space
}
printf("\n");
}
}
int main()
{
int mat_a[SIZE][SIZE] = {0}, mat_b[SIZE][SIZE] = {0}, result_mat[SIZE][SIZE] = {0}; // initializing matricies to {0}
printf("Please Enter Values For Matrix 1\n");
read_mat(mat_a);
printf("Please Enter Values For Matrix 2\n");
read_mat(mat_b);
mult_mat(mat_a, mat_b, result_mat);
printf("The First Matrix Is :- \n");
print_mat(mat_a);
printf("The Second Matrix Is :- \n");
print_mat(mat_b);
printf("The Resultant Matrix Is :- \n");
print_mat(result_mat);
return 0;
}
the outout I am getting:
enter image description here
thanks!
improve your print function
void print_mat(int mat[][SIZE])
{
int i, j, k = 0;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
printf("(%3d), ", &(mat[i][j])); // printing each value with a 3*space
}
printf("\n");
}
}
this program is trying to read in a 2D array (5x10) of char, and compress them. Such that (aaacccfeee -> a3c3f1e3). I have some trouble with my output, and I can't figure out where's the problem.
Here's the code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define SIZE1 5
#define SIZE2 10
void compress(char data[SIZE1][SIZE2]);
int main ()
{
int row, col;
char qn2[SIZE1][SIZE2];
printf("\nEnter your data (5x10 of characters): \n");
for (row = 0; row < SIZE1; ++row)
for (col = 0; col < SIZE2; ++col)
scanf("%c", &qn2[row][col]);
fflush(stdin);
compress(qn2);
return 0;
}
void compress(char data[SIZE1][SIZE2])
{
int row, col, count = 0;
char tempChar = data[0][0];
printf("\nThe compression output: \n");
for (row = 0; row < SIZE1; row++)
{
for (col = 0; col < SIZE2; col++)
{
if (data[row][col] == tempChar)
{
count ++;
}
else
{
printf("%c%d", tempChar, count);
tempChar = data[row][col];
count = 1;
}
} printf("\n");
}
}
Expected output:
Enter your data (5x10 of characters):
aaacccdeee
fffggghiii
jjjkkklmmm
nnnooopqqq
rrrssstuuu
The compression output:
a3c3d1e3
f3g3h1i3
j3k3l1m3
n3o3p1q3
r3s3t1u3
Output I got instead:
The compression output:
a3c3d1
e3f3g3h1
i3j3k3l1
m3n3o3p1
q3r3s3t1u3
Try this:
for (row = 0; row < SIZE1; row++)
{
for (col = 0; col < SIZE2; col++)
{
if (data[row][col] == tempChar)
{
count ++;
}
else
{
printf("%c", tempChar);
printf("%d", count);
tempChar = data[row][col];
count = 1;
}
}
}
if(count!=0) //need to print the last sequence of repeated characters.
{
printf("%c", tempChar);
printf("%d", count);
}