So I'm trying to write a program that makes matrix from random numbers and user can input the number of lines and columns
#include <stdio.h>
#include <stdlib.h>
int main() {
int c, x, n, m;
printf("write the number of lines\n");
scanf("%d", &n);
for (c = 1; c <= n; c++) {
x = rand() % 100 + 1;
printf("%d\n", x);
}
return 0;
}
This is what I have, I need it to generate columns, but I don't know how. m would be the number of columns
This is one of many ways to do it
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j, rows, columns;
int **matrix;
printf("write the number of rows\n");
scanf("%d", &rows);
printf("write the number of columns\n");
scanf("%d", &columns);
matrix = malloc(rows * sizeof *matrix);
if (matrix == NULL)
return -1;
for (i = 0 ; i < rows ; i++)
{
matrix[i] = malloc(columns * sizeof(int));
if (matrix[i] == NULL)
{
int k;
for (k = i ; k >= 0 ; k--)
free(matrix[k]);
free(matrix);
return -1;
}
for (j = 0 ; j < columns ; j++)
matrix[i][j] = rand() % 100;
}
for (i = 0 ; i < rows ; i++)
{
for (j = 0 ; j < columns ; j++)
printf("%5d", matrix[i][j]);
printf("\n");
}
for (i = 0 ; i < rows ; i++)
{
if (matrix[i] == NULL)
continue;
free(matrix[i]);
}
free(matrix);
return 0;
}
Related
I need to determine if a rectangular matrix has two rows of positive elements in C. I write part code for the set matrix and output its elements. I don't know how to check the positive elements in the row. Please help me
#include <stdio.h>
#include <conio.h>
#include <locale.h>
#define M 3
#define N 4
int main() {
setlocale(LC_ALL, "Rus");
float a[M][N]; //set matrix with 3 row and 4 column
int i, j; // row and column index
for (i = 0; i < M; i++) {
for (j = 0; j < N; j++)
scanf_s("%f", &a[i][j]);
}
for (i = 0; i < M; i++) {
printf("%d line:", i + 1);
for (j = 0; j < N; j++)
printf("%f", a[i][j]);
printf("\n");
}
_getch();
return 0;
}
So, I make some changes in my code after reading comments. Thanks a lot. But it's not working.
#include <stdio.h>
#include <conio.h>
#include <locale.h>
#define M 3
#define N 4
int main(){
setlocale(LC_ALL, "Rus");
float a[M][N]; //обьявление матрицы 3 строки и 4 столбца
int i, j; // индексы строки и столбца
int count;
for (i = 0; i < M; i++){
for (j = 0; j < N; j++)
scanf_s("%f", &a[i][j]);
}
for (i = 0; i < M; i++){
printf("%d-я строка:", i + 1);
for (j = 0; j < N; j++)
printf("%f", a[i][j]);
printf("\n");
}
count = 0;
for (i = 0; i < M; i++){
for (j = 0; j < N; j++)
if (a[i][j] > 0){
count++;
printf("%d", count);
}
}
_getch();
return 0;
}
What you need to do is to set a counter to 0 . Every time all the values are positive in row add 1 your counter. You can do that by a loop. If a value of your matrix a[i][j] is less than 0 just continue and move on to then next row.
The size of the square matrix is entered as standard input (the range of input values is 2 to 10). Create a program that prints the unit matrix of the input size.
The unit matrix is a matrix whose diagonal from the top left to the bottom right (main diagonal) is 1 and all others are zero.
ex) input: 5
result :
1 0 0 0 0\n
0 1 0 0 0\n
0 0 1 0 0\n
0 0 0 1 0\n
0 0 0 0 1\n
my code:
int main(){
int n;
scanf("%d", &n);
int **matrix = malloc(sizeof(int *) * n);
for (int i = 0; i < n; i++)
{
matrix[i] = malloc(sizeof(int) * n);
memset(matrix[i], 0, sizeof(int) * n);
}
for (int i = 0; i < n; i++)
{
matrix[i][i] = 1;
printf("%d ", matrix[i][i]);
}
printf("\n");
for (int i = 0; i < n; i++)
{
free(matrix[i]);
}
free(matrix);
return 0;
}
.. what 's the error?
You need a double loop to print the output:
for (int i = 0; i < n; i++)
{
matrix[i][i] = 1; // set identity
}
for (int j=0; j<n; j++)
{
for (int i = 0; i < n; i++)
{
printf("%d ", matrix[j][i]);
}
printf("\n");
}
One error is to think yo need a data strucure to print the output requested.
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int n;
if (scanf("%d", &n) != 1 || n < 2 || 10 < n) {
perror("Input error!\n\n");
return EXIT_FAILURE;
}
for (int i = 0; i < n; ++i) {
for (int k = 0; k < n; ++k)
printf(i == k ? "1 " : "0 ");
putchar('\n');
}
}
#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.
I need to move the largest number of randomly generated matrix to the right lower corner of matrix, but I don't know how. Can someone help.
This is my code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j, rows, columns;
int **matrix;
printf("write the number of rows\n");
scanf("%d", &rows);
printf("write the number of columns\n");
scanf("%d", &columns);
matrix = malloc(rows * sizeof *matrix);
for (i = 0 ; i < rows ; i++)
{
matrix[i] = malloc(columns * sizeof(int));
for (j = 0 ; j < columns ; j++)
matrix[i][j] = rand() % 100;
}
for (i = 0 ; i < rows ; i++)
{
for (j = 0 ; j < columns ; j++)
printf("%5d", matrix[i][j]);
printf("\n");
}
for (i = 0 ; i < rows ; i++)
free(matrix[i]);
free(matrix);
return 0;
}
Try the following
int max = matrix[0][0];
for (i = 0 ; i < rows ; i++)
{
for (j = 0 ; j < columns ; j++)
{
if ( max < matrix[i][j] ) max = matrix[i][j];
}
}
matrix[rows-1][columns-1] = max;
If you need to exchange the right lower element of the matrix with the maximum element then the code can look like
int max_i = 0;
int max_j = 0;
for (i = 0 ; i < rows ; i++)
{
for (j = 0 ; j < columns ; j++)
{
if ( matrix[max_i][max_j] < matrix[i][j] ) max_i = i, max_j = j;
}
}
int tmp = matrix[rows-1][columns-1];
matrix[rows-1][columns-1] = matrix[max_i][max_j];
matrix[max_i][max_j] = tmp;
You can generate the matrix with the requested feature this way
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j, rows, columns, largest, largestRow, largestColumn, swapValue;
int **matrix;
printf("write the number of rows\n");
scanf("%d", &rows);
printf("write the number of columns\n");
scanf("%d", &columns);
largest = 0;
largestRow = 0;
largestColumn = 0;
matrix = malloc(rows * sizeof *matrix);
if (matrix == NULL)
return -1;
for (i = 0 ; i < rows ; i++)
{
matrix[i] = malloc(columns * sizeof(int));
if (matrix[i] == NULL)
{
int k;
for (k = i - 1 ; k >= 0 ; k--)
free(matrix[k]);
free(matrix);
return -1;
}
for (j = 0 ; j < columns ; j++)
{
matrix[i][j] = rand() % 100;
if (matrix[i][j] > largest)
{
largestRow = i;
largestColumn = j;
largest = matrix[i][j];
}
}
}
swapValue = matrix[rows - 1][columns - 1];
matrix[rows - 1][columns - 1] = largest;
matrix[largestRow][largestColumn] = swapValue;
for (i = 0 ; i < rows ; i++)
{
for (j = 0 ; j < columns ; j++)
printf("%5d", matrix[i][j]);
printf("\n");
}
for (i = 0 ; i < rows ; i++)
free(matrix[i]);
free(matrix);
return 0;
}
I seem to get input for the first matrix, but when I ask the user to enter input for second matrix, the program crashes..why is this? cant figure it out, I even tried allocating memory, outcome is the same...
#include <stdlib.h>
#include <stdio.h>
#define MAXCOLUMNS 10
// dealing with 2D arrays, passing to function etc
void read_input(int (*a)[MAXCOLUMNS], int n_rows, int n_columns);
void print_sum (int (*a)[MAXCOLUMNS], int (*b)[MAXCOLUMNS], int (*c)[MAXCOLUMNS], int n_rows, int n_columns);
int main() {
int i;
int rows;
int columns;
int (*two_d_array)[MAXCOLUMNS];
int (*two_d_array2)[MAXCOLUMNS];
int (*output)[MAXCOLUMNS];
printf("enter the number of rows\n");
scanf("%d", &rows);
printf("enter the number of columns\n");
scanf("%d", &columns);
printf("enter data into array number 1\n");
read_input(two_d_array, rows, columns);
printf("enter data for 2d array number 2\n");
read_input(two_d_array2, rows, columns);
print_sum(two_d_array, two_d_array2, output, rows, columns);
return 0;
}
void read_input(int (*a)[MAXCOLUMNS], int n_rows, int n_columns) {
int i;
int j;
for (i = 0; i < n_rows; ++i) {
for (j = 0; j < n_columns; ++j) {
printf("enter details for rows number %d and column number %d\n", i + 1, j + 1);
scanf("%d", (*(a+i)+j));
getchar();
}
}
}
void print_sum (int (*a)[MAXCOLUMNS], int (*b)[MAXCOLUMNS], int (*c)[MAXCOLUMNS], int n_rows, int n_columns) {
int i;
int j;
// computing sum
for (i = 0; i < n_rows; i++) {
for (j = 0; j < n_columns; j++) {
*(*(c+i)+j) = *(*(a+i)+j) + *(*(b+i)+j);
}
}
// printing sum
for (i = 0; i < n_rows; i++) {
printf("\n");
for (j = 0; j < n_columns; j++) {
printf("%d\t", *(*(c+i)+j));
}
}
}
for C99
#include <stdlib.h>
#include <stdio.h>
#define MAXCOLUMNS 10
void read_input(int rows, int cols, int a[rows][cols]);
void print_sum (int rows, int cols, int in1[rows][cols], int in2[rows][cols], int out[rows][cols]);
int main(void) {
int i, rows, columns;
printf("enter the number of rows\n");
scanf("%d", &rows);
printf("enter the number of columns\n");
scanf("%d", &columns);
//if(columns > MAXCOLUMNS){ fprintf(stderr, "too large!"); return 1); }
int array1[rows][columns];
int array2[rows][columns];
int array3[rows][columns];
printf("enter data into array number 1\n");
read_input(rows, columns, array1);
printf("enter data for 2d array number 2\n");
read_input(rows, columns, array2);
print_sum(rows, columns, array1, array2, array3);
return 0;
}
void read_input(int rows, int cols, int a[rows][cols]){
int i, j;
for (i = 0; i < rows; ++i) {
for (j = 0; j < cols; ++j) {
printf("enter details for rows number %d and column number %d\n", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
}
}
void print_sum (int rows, int cols, int a[rows][cols], int b[rows][cols], int c[rows][cols]){
int i, j;
for (i = 0; i < rows; i++){
printf("\n");,,
for (j = 0; j < cols; j++){
c[i][j] = a[i][j] + b[i][j];
printf("%d\t", c[i][j]);
}
}
}
for int ** (2D_ARRAY)
#include <stdio.h>
#include <stdlib.h>
int main(void){
int rows = 3;
int cols = 5;
int **array;
int r, c;
//allocate
array = malloc(rows * sizeof(int*));
for(r = 0; r < rows ; ++r){
array[r] = malloc(cols * sizeof(int));
}
//set
for(r = 0; r < rows ; ++r){
for(c = 0; c < cols ; ++c){
array[r][c] = r * 10 + c;
}
}
//print
for(r = 0; r < rows ; ++r){
for(c = 0; c < cols ; ++c){
printf("%02d ", array[r][c]);
}
printf("\n");
}
}
for int (**)[SIZE]
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int main(void){
int rows = 3;
int cols = 5;
int (**array)[MAX];
int r, c;
//allocate
array = malloc(rows * sizeof(int (*)[MAX]));
for(r = 0; r < rows ; ++r){
array[r] = malloc(sizeof(int[MAX]));
}
//set
for(r = 0; r < rows ; ++r){
for(c = 0; c < cols ; ++c){
(*array[r])[c] = r * 10 + c;
}
}
//print
for(r = 0; r < rows ; ++r){
for(c = 0; c < cols ; ++c){
printf("%02d ", (*array[r])[c]);
}
printf("\n");
}
}
for int (*)[SIZE]
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int main(void){
int rows = 3;
int cols = 5;
int (*array)[MAX];
int r, c;
//allocate
array = malloc(rows * sizeof(int[MAX]));
//set
for(r = 0; r < rows ; ++r){
for(c = 0; c < cols ; ++c){
array[r][c] = r * 10 + c;
}
}
//print
for(r = 0; r < rows ; ++r){
for(c = 0; c < cols ; ++c){
printf("%02d ", array[r][c]);
}
printf("\n");
}
}