C program: Random matrix generator only saves last row (help appreciated!) - c

I am creating a program that manipulates a matrix.
Part of the program is that I need to generate a matrix with random inputs.
However, upon generating the matrix and printing each value of the matrix to double-check that the randomized numbers are being stored properly, the matrix seems to only be storing the last row of numbers, and then duplicating it.
Here is a screenshot to explain what I am referring to:
You can see that it creates the matrix [[3,6][7,5]] But it only shows it sores as [[7,5][7,5]]
And here is my code that isolates the problem:
#include <stdio.h>
#include <stdlib.h>
int main() {
int i, j, m, n, row, col;
int sum = 0, row_i=0, col_i=0;
int matrix[m][n];
int row_m[m];
printf("Enter m\n");
scanf("%d", &m);
printf("Enter n\n");
scanf("%d", &n);
for(i = 0; i < m; i++){
for(j = 0; j < n; j++){
matrix[i][j] = rand()%10;
printf("%d ",matrix[i][j]);
}
printf("\n");
}
printf("%d\n", matrix[0][0]);
printf("%d\n", matrix[0][1]);
printf("%d\n", matrix[1][0]);
printf("%d\n", matrix[1][1]);
return 0;
}

Instead of
int matrix[m][n];
int row_m[m];
printf("Enter m\n");
scanf("%d", &m);
printf("Enter n\n");
scanf("%d", &n);
use
printf("Enter m\n");
scanf("%d", &m);
printf("Enter n\n");
scanf("%d", &n);
int matrix[m][n];
int row_m[m];
to define your matrix and row_m after you got the values of m, n.

Related

Creating a Transposed Matrix in C

Creating a Program to print a Transposed Matrix
I'm creating a program in C where the user must define the number of rows and columns of a matrix, then the program must create random numbers and print the Transposed Matrix. I've achieved the first part, but my code isn't working for creating the Transposed Matrix. If it's a squared matrix, it must calculate also the D(mxn) * I(mxn), where D is the matrix and I the Identity matrix. Here`s the code:
#include <stdio.h>
#include <stdlib.h>
int main() {
unsigned short i, j, m, n;
int matriz[m][n], mTransposta[m][n], random;
printf("Entre a dimensao m da matriz: ");
scanf("%hu", &m);
printf("Entre a dimensao n da matriz: ");
scanf("%hu", &n);
printf("A matriz randomizada e:\n");
for(i = 0; i < m;i++) {
for(j = 0; j < n;j++) {
random= rand()%100;
matriz[m][n]= random;
printf("%i ", matriz[m][n]);
}
printf("\n");
}
printf("A matriz Transposta e:\n");
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
mTransposta[m][n]= matriz[n][m];
}
printf("%i ", matriz[m][n]);
}
printf("\n");
}
At least these problems
Declared too early
When int matriz[m][n] is declared, m, n do not have assigned values. Declare int matriz[m][n] after assigning m, n.
// int matriz[m][n], mTransposta[m][n], random;
printf("Entre a dimensao m da matriz: ");
scanf("%hu", &m);
printf("Entre a dimensao n da matriz: ");
scanf("%hu", &n);
int matriz[m][n], mTransposta[m][n], random;
Transposed array
This implies mTransposta[m][n] should be mTransposta[n][m]
Print more often
printf("%i ", matriz[m][n]); only prints in an outer loop. Expect that in an inner loop.
Unused mTransposta[m][n]
Code never reads the values in mTransposta[m][n]=.
Other
scanf() return values not checked.
D(mxn) * I(mxn) code missing.
Identity matrix never formed.
Printing a space after the last number is poor form. Alternative:
const char *sep = "";
for(unsigned j = 0; j < n;j++) {
matriz[m][n] = rand()%100;
printf("%s%i", sep, matriz[m][n]);
sep = " ";
}
printf("\n");

A function that prints a matrix row

I'm trying to write a function that prints a matrix row chosen by user. It works for the first row of the matrix but it doesn't work for other rows.
Here's my code.
row: the row we want to print
n: number of the rows/columns in matrix
Matrix (nxn)
Code:
#include <stdio.h>
#define SIZE 50
void prow(float *arr, int row, int n){
int i;
for(i = row * n; i < (row * n) + n; i++){
printf("%f ", *(arr+i));
}
}
int main(){
int n, i, j;
float matrix[SIZE][SIZE];
printf("Row / column number: ");
scanf("%d", &n);
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
printf("[%d][%d] element: ", i, j);
scanf("%f", &matrix[i][j]);
}
}
prow(&matrix[0][0], 2, n);
return 0;
}
The compiler already knows your array is 50 x 50 and nothing else
#define SIZE 50
float matrix[SIZE][SIZE];
but you are (probably?) using the array as if it had a different size, because you input n as size. So when you assign the entries,
scanf("%f", &matrix[i][j]);
they aren't being put in the right places (each row is still 50 entries according to the compiler, despite n being something else).
Make sure n and SIZE match.

C Program that reads and Stores a series of integers

How do I write a program that reads and stores a series of integer, first ask for N,then read the value into an array, and calculate the sum of the first N values?
void main() {
int N,i,sum=0,temp;
printf("Please enter value of N\n");
scanf_s("%d", &N);
int *arr = (int*)malloc(sizeof(int)*N);
for (i = 0; i < N; i++) {
printf("Please %d value of array \n",i+1);
scanf_s("%d", &temp);
arr[i] = temp;
sum += temp;
}
printf("Sum Of N Elemetes In Array Is %d", sum);
}

Couldn't get the reason for the fault in the code

This is a program to print the smallest value and its position in an array (defined by user).
#include <stdio.h>
int position_smallest(int a[],int n)
{
int smallest = a[0];
int i,k;
for(i=0; i<=n-1; i=i+1)
{
if(a[i]<a[0])
{
smallest = a[i];
k = i;
}
}
printf("The smallest value is %d\n", smallest);
printf("It's position is %d\n", k);
return 0;
}
int main()
{
int n,j;
int a[n];
printf("Enter the size of the array: ");
scanf("%d", &n);
for(j=0; j<=n; j=j+1)
{
printf("a[%d] = ", j);
scanf("%d", &a[j]);
}
position_smallest(a,n);
}
But upon running it, it shows following error:
Segmentation fault (core dumped)
What can be the possible reason(s) for it?
First error, as stated in one of the comments, is declaring an array of size n before even knowing how much n is.
Second mistake is for loop in your main function that goes from 0 to n, i.e. index of an array is out of bounds.
Try this:
int main() {
int n = 0, j = 0;
printf("Enter the size of the array: ");
scanf("%d", &n);
int a[n];
for (j = 0; j < n; j++) {
printf("a[%d] = ", j + 1);
scanf("%d", &a[j]);
}
position_smallest(a,n);
}
If this solved your problem, please mark it.
First of all:
int n, j;
both uninitialized. Initialize them otherwise you will get garbage values.
int n = 0, j = 0;
What happens if n by chance (very likely) is 0 in following line?
int a[n];
You allocate 0 bytes for array a[]. You then enter 10 in following line
scanf("%d", &n);
You will get segmentation fault in for() loop below because your loop is trying to put 10 elements where you allocated no memory at all.
What happens if uninitialized n by chance (very likely) is 2^32 * 4 bytes (ints max)?
int a[n];
You allocate 2^32 bytes for array a[]. You then enter 10 in following line
scanf("%d", &n);
You will not get segmentation fault but you will allocate 2^32 * 4 bytes of memory for your program and you will use only 10
Second if that is not enough:
for (j = 0; j <= n; ++j)
scanf("%d", arr[n];
will access 11th element of array which is undefined behavior, and you might even get segmentation fault there. As you know arrays in C arrays are indexed from 0 to n - 1 (if n is size of array).
Third your loop inside function:
for(i=0; i<=n-1; i=i+1)
is the same as:
for(i=0; i < n; ++i)
And finally, you have a bug in your code, I believe:
if(a[i]<a[0])
should be:
if (a[i] < smallest)
because it is most likely that you would like compare other number to already smallest element not to a[0]. Here is my code
#include <stdio.h>
int position_smallest(int a[],int n) {
int smallest = a[0];
int i = 0, k=0;
for(i=0; i<n; ++i) {
if(a[i]<smallest) {
smallest = a[i];
k = i;
}
}
printf("The smallest value is %d\n", smallest);
printf("It's position is %d\n", k);
return 0;
}
int main() {
int n=0, j=0;
printf("Enter the size of the array: ");
scanf("%d", &n);
int a[n];
for(j=0; j<n; ++j) {
printf("a[%d] = ", j);
scanf("%d", &a[j]);
}
position_smallest(a,n);
return 0;
}
The version above is legit for C99 and up standards. If you are using C89 and earlier compilers you are stack with fixed size as mentioned in #Saurabh's answer or preferably use malloc().
Here is malloc() version:
#include <stdio.h>
#include <stdlib.h>
int position_smallest(int a[],int n) {
int smallest = a[0];
int i=0,k=0;
for(i=0; i<=n-1; i=i+1) {
if(a[i]<smallest) {
smallest = a[i];
k = i;
}
}
printf("The smallest value is %d\n", smallest);
printf("It's position is %d\n", k);
return 0;
}
int main() {
int n=0, j=0;
printf("Enter the size of the array: ");
scanf("%d", &n);
int *a = malloc(sizeof(int) * n);
for(j=0; j<n; j=j+1) {
printf("a[%d] = ", j);
scanf("%d", &a[j]);
}
position_smallest(a,n);
return 0;
}
Use this one:
#include <stdio.h>
int position_smallest(int a[],int n)
{
int smallest = a[0];
int i,k;
for(i=0; i<=n-1; i=i+1)
{
if(a[i]<a[0])
{
smallest = a[i];
k = i;
}
}
printf("The smallest value is %d\n", smallest);
printf("It's position is %d\n", k);
return 0;
}
int main()
{
int n,j;
int a[100];
printf("Enter the size of the array: ");
scanf("%d", &n);
for(j=0; j<n; j=j+1)
{
printf("a[%d] = ", j);
scanf("%d", &a[j]);
}
position_smallest(a,n);
}
Specify the size of the array anything but run the loop according to user's input.
And there is some problems in your function position_smallest. So if you want correct it then take a look at #Gox's answer.

Array output difficulties

I have been practising with arrays for a bit and I have encountered a problem I can't seem to find the answer for. I am trying to display the numbers the user enters, however they are not turning out as I expected. It should be in a form of a column.
#include <stdio.h>
int main (void)
{
double A[5], B[5];
int i;
for (i=0; i<=4; i++)
{
printf("Enter 5 numbers for column A: ");
scanf("%lf", &A[i]);
}
for (i=0; i<=4; i++)
{
printf("Enter 5 numbers for column B: ");
scanf("%lf", &B[i]);
}
printf("A = (%f) B = (%f) \n", A[i], B[i]);
return 0;
}
The printf statement seems to be correct however numbers are not showing in the output.
You should ask yourself, what is the value of i, when printing the final output.
You should also ask yourself, what is in array A and B at index i.
Given these are understood, we can display the content of an array in the same fashion as it is filled.
#include <stdio.h>
int main (void)
{
double A[5], B[5];
int i;
for (i=0; i<=4; i++)
{
printf("Enter 5 numbers for column A: ");
scanf("%lf", &A[i]);
}
for (i=0; i<=4; i++)
{
printf("Enter 5 numbers for column B: ");
scanf("%lf", &B[i]);
}
for (i=0; i<=4; i++)
{
printf("A = (%f) B = (%f) \n", A[i], B[i]);
}
return 0;
}
As said by #Tsakiroglou Fotis, you forgot to add bracket after the main function and also you are not looping the final print statement to print all the elements. Try using editors that does take care of such mistakes. here is your corrected code
#include <stdio.h>
int main (void){
double A[5], B[5];
int i;
for(i=0; i<=4; i++)
{
printf("Enter 5 numbers for column A: ");
scanf("%lf", &A[i]);
}
for(i=0; i<=4; i++)
{
printf("Enter 5 numbers for column B: ");
scanf("%lf", &B[i]);
}
for(i=0; i<5; i++){
printf("A = (%f) B = (%f) \n", A[i], B[i]);
}
return 0;
}

Resources