I have been trying to create an NxN square matrix (the size of the square matrix is determined by the user) but seem to have run into an issue. The displayed matrix is actually a symmetric matrix which is not what I am trying to as I want to be able to have each value hold a unique integer. Any help is greatly appreciated. Here's my code:
#include <stdio.h>
int main()
{
int i;
int j;
int size = 1;
int array [size][size];
printf ("Enter the size you would like for the nxn matrix: ");
scanf ("%d", &size);
printf ("Now enter elements into the matrix. \n");
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf ("[%d][%d] = ", i, j);
scanf ("%d", &array [i][j]);
}
}
printf ("------------------------\n\n");
printf ("the matrix is: \n\n");
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf (" %d ", array [i][j]);
}
printf ("\n");
}
return (0);
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
// Create variables
int i, j, size;
// Create 2D array
printf ("Enter the size you would like for the nxn matrix: ");
scanf ("%d", &size);
int *array = (int *)malloc(size * size * sizeof(int));
// Fill 2D array with values
printf ("Now enter elements into the matrix. \n");
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf ("[%d][%d] = ", i, j);
scanf ("%d", &array[i * size + j]);
}
}
printf ("------------------------\n\n");
// Display values of 2D array
printf ("the matrix is: \n\n");
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf ("%d", array[i * size + j]);
}
printf ("\n");
}
free(array);
return (0);
}
Related
So I have been struggling with this for quit some time and I'm just not able to figure out how I'm supposed to flatten this randomly generated 5*15 2D array into a 1D and print it without using malloc but instead just for loops. And I'm slowly getting unhinged
This is the code for the printing random array.
#include <stdio.h>
#include <stdlib.h>
int main() {
unsigned short i, j;
int ar[5][15];
puts("The array: ");
for(i = 0; i < 5;i++) {
for(j = 0; j < 15;j++) {
printf("%d ", rand()%10);
}
printf("\n");
}
return 0;
}
If you just want to print them as if it's 1D-array just comment printf("\n"); in the outer for loop.
But, if you want the data from 2D-array in an 1D-array, you've to copy them individually.
#include <stdio.h>
#include <stdlib.h>
#define ROWS 5
#define COLS 15
int main() {
unsigned short i, j;
int ar[ROWS][COLS];
int flat_array [ROWS * COLS];
puts ("2D array: ");
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
ar[i][j] = rand() % 10;
flat_array[COLS * i + j] = ar[i][j];
printf ("%d ", ar[i][j]);
}
printf ("\n");
}
printf("\nFlattened Array: \n");
for (int fi = 0; fi < ROWS * COLS; )
printf ("%d ", flat_array[fi++]);
return 0;
}
i.e. If you've a 2D array of size [M][N] and want to flatten it to an 1D array of size [M*N]
int Arr_2D[M][N]; // with data
int Arr_1D[M*N];
for (int ri = 0; ri < M; ++ri)
for (int ci = 0; ci < N; ++ci)
Arr_1D[ri * N + ci] = Arr_2D [ri][ci];
Also, Data in true-2D-array is stored in memory contiguously, as if a true-1D-array. So :
#include <stdio.h>
#include <stdlib.h>
#define ROWS 5
#define COLS 15
int main() {
int Arr_2D[ROWS][COLS];
printf ("Original 2D Array: \n");
for (int ri = 0; ri < ROWS; ++ri) {
for (int ci = 0; ci < COLS; ++ci) {
Arr_2D[ri][ci] = rand() % 10;
printf ("%d ", Arr_2D[ri][ci]);
}
printf ("\n");
}
printf ("\nAccessing a 2D array as 1D array: \n");
int *AP_1D = (int *) Arr_2D;
for (int ni =0; ni < ROWS * COLS; )
printf ("%d ", AP_1D[ni++]);
printf ("\n");
return 0;
}
There is also, Row & Column major memory storage, we digress.
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
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;
}
I need to make some functions for 2-dimensional arrays (a.k.a. matrixes) and the problem just starts with the first one.
I allocated the matrix in the heap memory using malloc() and then I tried to make a function that initializes the matrixes using a for loop of scanf()s, but after 3 times it takes the input, the program crashes.
Here is the code I made
void initMatrix(int **mat,int row,int col)
{
for(int i=0;i<row;i++){
printf("\n");
for(int j=0;j<col;i++){
printf("Cell (%d,%d): ",i,j);
scanf("%d",&mat[i][j]);
}
}
}
int main()
{
int r,c;
printf("Number of rows: ");
scanf("%d",&r);
printf("Number of columns: ");
scanf("%d",&c);
int **arr = (int **)malloc(sizeof(int*) * r);
for(int i=0;i<r;i++)
arr[i]=(int*)malloc(sizeof(int) * c);
// int count=0;
// for (int i = 0; i < r; i++)
// for (int j = 0; j < c; j++)
// arr[i][j] = ++count;
for (int i = 0; i < r; i++){
printf("\n");
for (int j = 0; j < c; j++)
printf("%d ", arr[i][j]);
}
initMatrix(arr,r,c);
printf("\n");
free(arr);
}
If I manually insert the content of the matrix, the program does work (without the initmatrix() function), I don't know why… I probably made a mistake somewhere.
I have the following code which is driving me nuts. I am trying to input values into a 2-dimensional array.
I do not really know how I can insert a value in to specific location in the array. For example I want to enter the values in a pyramid fashion. I should be able to add one more value in each level.
a[0][0] ===> 1
a[1][0] & [1][1] &[1][2] ===> 2 3
a[2][0] & [2][0] & [2][2] ===> 4 5 6 etc.
I also want to be able to store the greatest integer In each level so that I can sum all the large integers in each level.
So far with the following code, I am unable to figure out how to insert the value and I also cannot sum the greatest values in each level.
for(i = 0; i < 2; i++){
for(j = 0; j <= 4; j++){
printf("Enter the values in to the array");
scanf("%d",&arr[i][j]);
}
}
for(i = 0; i < 2; i++){
for(j = 0; j <= 4; j++){
if(arr[i][j] > arr[i][j+1]){
holder = arr[i][j];
}else{
holder = arr[i][j+1];
}
}
sum = sum+holder;
}
printf("%d\n\n",sum);
To enter the values in a pyramid fashion & calculate the sum of the largest numbers of each row, try the code below:
int arr[5][10];
int i,j;
int holder=0;
int sum = 0;
for (i=0;i<5;i++)
{
for(j=0; j<=i;j++)
{
printf("Enter your input: \n");
scanf("%d", &arr[i][j]);
if(arr[i][j] > holder)
{
holder = arr[i][j];
}
}
printf("Largest Value in Row %d is %d\n", i, holder);
sum = sum + holder;
holder = 0;
}
printf(" Sum = %d\n", sum);
Try the code below for dynamically deciding the array size and assigning the values:-
int **arr;
int *greatest_each_lavel;
int n, i, j, temp;
printf ("enter the level.\n");
scanf ("%d", &n);
if (n<1)
{
printf ("Wrong input.\n");
exit (1);
}
greatest_each_level = (int *) malloc (n * sizeof (int));
arr = (int **) malloc (n * sizeof (int *));
for (i=1; i<=n; i++)
{
arr[i] = (int *) malloc (i * sizeof (int));
}
// Populate the array members.
for (i=0; i<n; i++)
{
temp = 0;
for (j=0; j<=i; j++)
{
printf ("Enter the number ");
scanf ("%d", &arr[i][j]);
if (arr[i][j] > k)
{
temp = arr[i][j];
}
}
greatest_each_level[i] = temp;
}
// To get the sum of greatest number at each level
int sum = 0;
for (i = 0; i < n; i++)
sum = sum + greatest_each_level[i];
printf ("Sum is : %d\n", sum);
// When all task are done, dont forget to free the memory.
for (i=0; i<n; i++)
{
free (arr[i]);
}
free (arr);
free (greatest_each_level);
greatest_each_level = NULL;
arr = NULL;