To printf a matrix - c

When i want to print a matrix which i input,i can use code:
#include <stdio.h>
int main(void)
{
int n, m; //row and column
printf("Enter row and column:\n");
scanf("%d %d", &n, &m);
int x[n][m];
printf("Enter your matrix:\n");
for (int i = 0; i < n; i++) //input my matrix
{
for (int j = 0; j < m; j++)
{
scanf("%d", &x[i][j]);
}
}
printf("print it:\n");
for (int i = 0; i < n; i++) //print it
{
for (int j = 0; j < m; j++)
{
printf("%d ", x[i][j]);
}
putchar('\n');
}
}
enter image description here(a possible case)
In code above, I have to assign values to the rows and columns of the matrix,which named "n" and "m".
int n, m;
scanf("%d %d", &n, &m);
But now I am asking a way to automatic tally .
Can I get this one directly?
enter image description here

You can simulate a two-dimensional array with a one-dimensional array, if that's what you mean:
#include <stdio.h>
#include <stdlib.h>
#define DIGITS 3
int main(void) { // It's good practice to fill function arguments with void if not planning on using them
/* scanf("%d %d", &n, &m); Using scanf with uninitialised variables will result in
* undefined behaviour if it is unable to convert the input. Using fgets
* is easier to debug, safer, and cleaner.
* I highly recommend this: http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html
*/
char buf[DIGITS+1];
printf("%s", "Enter array rows:");
fgets(buf, DIGITS+1, stdin);
const int n = atoi(buf);
printf("%s", "Enter array columns:");
fgets(buf, DIGITS+1, stdin);
const int m = atoi(buf);
int x[n*m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
printf("Enter array value at %d, %d: ", i, j);
fgets(buf, DIGITS+1, stdin);
x[i+j] = atoi(buf);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
printf("%d ", x[i+j]);
}
printf("\n");
}
}
I'm not really sure as to why you would do this when C supports your two-dimensional array answer equally.

But now I am asking a way to automatic tally. Can I get this one directly?
Yes.
Form a linked-list of lines. Initially the list is empty.
Read the first line of input, the "1 2 3" into a string. Use fgets().
Parse the line to detect the number of values in it.
Append the line to the linked list of lines.
Continue doing so until 1) end-of-file, 2) a blank line or 3) number of integer is not the same as the first (error condition).
Now code has the m (number of values per line) and n, the number of lines.
Form int x[n][m];
Parse the lines for values and save in x.

Related

How can I find the max of every 5 elements of an array in C

So I need to write a program which takes (by user input) a integer array of 100 elements and finds the max of every 5 elements and writes them in a new array and adds the rest of the numbers to another array.
So an example would be:
original array (array1): 1,23,6,7,16,19,24,56,99,43 ...
soo it will take the first 5 elements: 1,23,6,7,16 and find the max value (23) and add it to the new array
then the next 5 : 19,24,56,99,43 find their max (99) and add it to the other array and so on until it reaches the last number.
second array (array2): 23,99
third array (array3): 1,6,7,16,19,24,56,43
I've tried everything, but i can only get either the Max number in the WHOLE array or the max number of the FIRST 5 elements.
This is how my program should look like:
#include <stdio.h>
#include <stdlib.h>
int array1[100];
int array2[100];
int size;
int main() {
printf("Enter the size of the array: ");
scanf("%d", &size);
if (size > 100) {
return 0;
}
printf("\nEnter %d numbers: \n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &array1[i]);
}
printf("\nEntered array: ");
for (int i = 0; i < size; i++) {
printf("%d, ", array1[i]);
}
printf("\n");
// the code for the max numbers should be here
printf("The max values of every 5 elements are: \n");
for(int i = 0; i < size2; i++) {
printf("%d, ", array2[i]);
}
}
"size2" is the size of the new array.
#include <stdio.h>
#include <stdlib.h>
int array1[100];
int array2[100];
int array3[100];
int size, size2, size3;
int main() {
printf("Enter the size of the array: ");
scanf("%d", &size);
if (size > 100) {
return 0;
}
printf("\nEnter %d numbers: \n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &array1[i]);
}
printf("\nEntered array: ");
for (int i = 0; i < size; i++) {
printf("%d", array1[i]);
if(i%5==4) printf("| ");
else printf(", ");
}
printf("\n");
// the code for the max numbers should be here
size2=0; size3=0;
int maxvalue_index=0;
for(int i = 1; i < size; i++) {
if(array1[i]>array1[maxvalue_index]) maxvalue_index=i;
if(i%5==4 || i==size-1){
array2[size2]=array1[maxvalue_index];
size2++;
for(int j=i-(i%5); j<=i ; j++){
if(array1[j]==array1[maxvalue_index]) continue;
array3[size3]=array1[j];
size3++;
}
maxvalue_index=i+1;
}
}
printf("The max values of every 5 elements are: \n");
for(int i=0;i<size2;i++) printf("%d ",array2[i]);
printf("\nArray3\n");
for(int i=0;i<size3;i++) printf("%d ",array3[i]);
printf("\n");
}
Try this, It will work for non 5*n lengths
Let me know if there is any error :)
The function below finds the maximum of elements in the array from start to end exclusive.
int findmax(int *const arr, const size_t len, const size_t begin, size_t end) {
int res = arr[begin];
end = (len < end) ? len : end;
for (int i = begin+1; i < end; i++) {
if (arr[i] > res)
res = arr[i];
}
return res;
}
Define this function before your main, then call it for every five elements; the maximum of the first five elements is findmax(array1, size1, 0, 5), for the next
five it would be findmax(array1, size1, 5, 10) and so on and so forth. It shouldn't take that much effort to make a for loop that automates the process.

Converting matrix to an array in C

How can I copy the elements from a matrix,entered by user,to an array? I tried this, but it didn't work:
#include<stdio.h>
int main(){
int m[20][20],a[400],c=0;//max dimensions;
scanf("%d %d",&M,&N);//dimensions of matrix;
for(i=0;i<M;i++{
for(j=0;j<N;j++{
scanf("%d", &m[i][j]);
for(c=0;c<M*N;c++)
a[c]=m[i][j];
}}}
Don't know why you want to store both the matrix format and the array format, but anyway here is a code that should do the trick with also the data output to show the result:
#include <stdio.h>
#include <stdlib.h>
#define R 20 //max rows
#define C 20 //max columns
int main() {
int m[R][C]; //matrix
int a[R*C]; //array
int r, c; //user matrix size
int i, j; //iterators
printf("insert row size: ");
scanf("%d", &r);
printf("insert column size: ");
scanf("%d", &c);
if(r > R || c > C) {
printf("Invalid sizes");
return -1;
}
for(i = 0; i < r; i++) {
for(j = 0; j < c; j++) {
printf("insert value for row %d column %d: ", i + 1, j + 1);
scanf("%d", &m[i][j]);
a[(c * i) + j] = m[i][j];
}
}
for(i = 0; i < r; i++) {
for(j = 0; j < c; j++) {
printf("%d ", m[i][j]);
}
printf("\n");
}
for(i = 0; i < r * c; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
Note that I also added some checks for the user data, avoiding to generate a matrix bigger that the maximum size. Also you don't need separate loops but it can be done all together.
Also please post a code that is compilable and can be run, as explained here: https://stackoverflow.com/help/how-to-ask

Check if the first and last row of a matrix has only negative values

i have some problem making this program
I have created two arrays where I go to insert the first and the last line, then I check if every element is > 0 but it doesn't seem to work..
That's my code:
int main()
{
int i, j, n, m;
int matrix[10][20];
int first_row[m];
int last_row[m];
printf("Enter number of rows : ");
scanf("%d", &n);
printf("Enter number of columns : ");
scanf("%d", &m);
/* Input data in matrix */
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
printf("Enter data in [%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
if(matrix[i=0][j]) // First row
first_row[i] = matrix[i=0][j];
if(matrix[i=n-1][j]) // second row
last_row[i] = matrix[i=n-1][j];
}
}
for(i=0;i<n;i++)
{
for (j=j+1;j<n;j++)
{
if(last_row[i] < 0)
printf("Negative element");
}
}
}
I assume in the if conditions matrix[i=0][j] and matrix[i=n-1][j] was to check if the current row being input is the first or last row respectively. If that was the case then you just need to simply check if i is 0 (i == 0) or n - 1 (i == n-1) instead of using matrix[i=0][j] and matrix[i=n-1][j].
Also the line first_row[i] = matrix[i=0][j]; and last_row[i] = matrix[i=n-1][j]; will update i which is what you should avoid in a for loop where i is index. If you intended to assign values to first_row and last_row, you should change them to first_row[j] = matrix[0][j]; and last_row[j] = matrix[n-1][j]; to a get the desire result (note that j should be used for indexing first_row and last_row instead of i because j represents matrix column).
If you want to check every element in the matrix for negative values, then the for loop for (j=j+1;j<n;j++) should be changed to for (j=0;j<m;j++) and matrix[i][j] should be used instead of last_row[i].
Edit: Also as #chux suggested, you should consider initializing matrix, first_row and last_row arrays after you input n and m in order to avoid segmentation fault for any n and m values that are larger than 10 and 20 respectively.
#include <stdio.h>
int main()
{
int i, j, n, m;
printf("Enter number of rows : ");
scanf("%d", &n);
printf("Enter number of columns : ");
scanf("%d", &m);
int matrix[n][m];
int first_row[m];
int last_row[m];
/* Input data in matrix */
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
printf("Enter data in [%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
if(i == 0) // First row
first_row[j] = matrix[0][j];
if(i == n-1) // second row
last_row[j] = matrix[n-1][j];
}
}
for(i=0;i<n;i++)
{
for (j=0;j<m;j++)
{
if(matrix[i][j] < 0)
printf("Negative element %d\n", matrix[i][j]);
}
}
}

EOF and free part of unused 2d array

My background is java so I'm not used to c syntax yet.
I need to do the following: let the user to input number k (number of rows), and after to insert values into 2d array with this form:
1 2
3 4
5 6
i.e two values with space between them and then new line for the new row.
If the user entered k=1000 but entered only 4 rows so the function call would be only with the array with 4 rows and not 100. the loop that reads the values should stop if: there are k rows or reaching to EOF
My questions:
I don't know how to implement the EOF part.
I don't know how to implement that for k=1000 and there only 4 rows so call the function with the array that contains only 4 rows
Here my code:
#include <stdio.h>
#define COLS 2
void foo(int** rows, int n);
int main()
{
int k;
printf("Please enter number of rows\n");
scanf_s("%d", &k);
int** matrix = (int**)malloc(k * sizeof(int*));
for (int i = 0; i < k; i++)
matrix[i] = (int*)malloc(COLS * sizeof(int));
int num1, num2;
for (int i = 0; i < k||num1!=EOF; i++)
{
printf("Enter two numbers separated by space \n");
scanf_s("%d %d", &num1, &num2);
matrix[i][0]=num1;
matrix[i][1] = num2;
}
printf("The array:: \n");
for (int i = 0; i < k; i++)
{
for (int j = 0; j < COLS; j++)
{
printf("%d \t",matrix[i][j]);
}
printf("\n");
}
foo(matrix, k);
for (int i = 0; i < k; i++)
{
free(matrix[i]);
}
free(matrix);
return 0;
}
void foo(int** rows, int n)
{
//some stuff
}
Change the bellow portion of your code:
for (int i = 0; i < k||num1!=EOF; i++)
{
printf("Enter two numbers separated by space \n");
scanf_s("%d %d", &num1, &num2);
matrix[i][0]=num1;
matrix[i][1] = num2;
}
To:
int i;
for (i = 0; i < k; i++)
{
printf("Enter two numbers separated by space \n");
if(2 != scanf_s("%d %d", &num1, &num2)) break;
matrix[i][0]=num1;
matrix[i][1] = num2;
}
k = i;
Hope it will work as you want
check the return value of scanf
for (int i = 0; i < k; i++)
{
printf("Enter two numbers separated by space \n");
if(scanf_s("%d %d", &num1, &num2)==EOF)
break;
matrix[i][0]=num1;
matrix[i][1] = num2;
}

How do I print a pyramid with text based on number given by user?

Currently stuck on creating a pyramid out of hashes (#'s) based on a number given by user input. The example for CS50 only describes how to create a square based on the number given.
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int n = get_int("Number:\n");
if(n>0 && n<9)
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
printf("#");
}
printf("\n");
}
}
Expected result is to create a pyramid that is x amount of #'s wide and tall based on the input given by user.
Actual result is a square that is x amount of #'s wide and tall based on the input given by user.
You need a loop which prints spaces until the second loop's counter (j) is less than n-i. Please see below:
#include <stdio.h>
int main(void)
{
int n, i, j, k;
printf("Number: ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
for (j = 0; j < n-i; j++)
{
printf(" ");
}
for (k =0; k <= i; k++)
{
printf("# ");
}
printf("\n");
}
} // end main function

Resources