Array output difficulties - c

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;
}

Related

Function ain't printing the correct matrix after adding

I'm trying to do a code in C with simple functions that can add two given matrixes (bidimensional arrays),, in order to make a matrix calculator.
The thing is, for some reason the code keeps printing the second matrix instead of the final one.
We have three matrixes (1,2,3) and user gives the value of 1 and 2, to store it and add them to print matrix3. When executing, it will only give the values of the second matrix. I hope someone could tell me why or give any suggestions pls.
#include <stdio.h>
int r1,c1,r2,c2,i,j, matrix1[100][100], matrix2[100][100], finalmatrix[100][100]; //r stands for rows, c for columns and i,j are iterarions
void fillMatrix(){ //Function that creates two bidimensional arrays and asks for its parameters
printf("Enter the number of rows of first matrix: \n");
scanf("%d", &r1);
printf("Enter the number of columns of first matrix: \n");
scanf("%d", &c1);
printf(" First matrix sizes are:[%d][%d] \n", r1,c1); //This is only the print of rows and columns values of matrix1
int matrix1[r1][c1];
printf("\nEnter elements of 1st matrix:\n"); //asks for the values of the matrix
for (i = 0; i < r1; ++i)
for (j = 0; j < c1; ++j) {
printf("Enter element a%d%d: \n", i + 1, j + 1);
scanf("%d", &matrix1[i][j]);
}
printf("Enter the number of rows of second matrix: \n");
scanf("%d", &r2);
printf("Enter the number of columns of second matrix: \n");
scanf("%d", &c2);
printf("Second matrix sizes are:[%d][%d] \n", r2,c2); //This is only the print of rows and columns values of matrix1
printf("\n Enter elements of 2nd matrix: \n");
for (i=0; i< r2; i++)
for (j=0; j<c2; j++){
printf("Enter the element a%d%d: \n", i+1,j+1);
scanf("%d", &matrix2[i][j]);
}
}
void addMatrix()
{
//Initialize matrix3=0
for(i=0; i<r1; i++)
{
for (j=0; j<c2; ++j)
{
finalmatrix[i][j]=0;
}
}
//Add matrix1 to matrix2 and store in matrix3
for (i=0; i<r1; i++)
{
for (j=0; j<c2; j++)
{
finalmatrix[i][j]=matrix1[i][j] + matrix2[i][j];
}
}
//print matrix3
printf("The resulting matrix is: \n");
for (i=0; i<r1; i++)
{
for (j=0;j<c2; j++)
{
printf("%d ", finalmatrix[i][j]);
if (j==c2-1){
printf("\n \n");}
}
}
}
int main(){
fillMatrix();
addMatrix();
}
Remove the declaration of matrix1 in the fillMatrix function that hides the global matrix1.
int matrix1[r1][c1]; // remove this line

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

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.

I'm trying to add numbers to each new line

I am trying to print out a sequence of numbers and * that form a rectangle depending on the number provided by the user. I managed to print a rectangle with * but I do not know how to incorporate numbers to print out something like:
1*****
12****
123***
1234**
12345*
123456
This is what I have:
#include <stdio.h>
int main{
int i, j;
int num;
printf("Enter a number from 1-9: ");
scanf("%d", &num);
for(i=1; i<=num; i++)
{
for(j=1; j<=num; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Hope this helps!
#include <stdio.h>
int main(){
int i, j;
int num;
printf("Enter a number from 1-9: ");
scanf("%d", &num);
for(i=1; i<=num; i++)
{
for(j=1; j<=i; j++)
{
printf("%d",j);
}
for(;j<=num;j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Also,Instead for using 2 loops inside of Master loop,You can Use conditional operators to manipulate output!
Cheers! :)

Correct Alignment of Numbers in PASCAL TRIANGLE

I made a program for making a pascal triangle and for the input of numbers ( rows ) > 5 , there is an alignment problem i.e for ncr > 10. Help me out please.
I have included the images for output of the program.
Output Image
#include<stdio.h>
int factorial(int number)
{
int fact=1;
for(int i=1; i<=number; ++i )
{
fact*=i;
}
return fact;
}
int ncr(int n, int r)
{
int ncr;
int fact1=factorial(n);
int fact2=factorial(n-r);
int fact3=factorial(r);
ncr = fact1 /(fact2 * fact3);
return ncr;
}
int main()
{
int rows;
printf("enter the number of rows :\n");
scanf("%d",&rows);
for(int n=0; n<rows; n++)
{
for(int i=1; i<=rows-n; i++)
{
printf(" ");
}
for(int r=0; r<=n; r++)
{
printf("%d ",ncr(n,r));
}
printf("\n");
}
return 0;
}
You can change the inner loop like this
for(int i=1; i<=rows-n; i++)
{
printf(" "); // Note the extra space
}
for(int r=0; r<=n; r++)
{
printf("%3d ",ncr(n,r)); // Changed to %3d
}
This will work upto 9 rows. If you want it to work for more rows, you can add another space in the first printf and change the second printf to %5d
printf can take a precision before the formatter. Change printf("%d ",ncr(n,r)); to printf("%3d ",ncr(n,r)); to make the numbers 3 characters wide. Also change printf(" "); to printf(" ");.
If you use
printf ("Width trick: %*d \n", 5, 10);
this will add 5 more spaces before the digit value.

Transferring contents of an array to another in C

I'm coding a program which accepts a number as a divisor and the other numbers from the user. My problem is with segregating the array where the ten entered numbers into two different arrays, one array is for numbers divisible by a divisor entered by the user and one is for non-divisible ones. I think I've got most of it down but whenever I try to display the contents of the array it would show a 0 at the end of the line. Also when none of the entered numbers are divisible it would dispaly "16 0 1" even if those numbers are not entered by the user.
Here's my code:
int main(){
int num, arr[size], div[size], nondiv[size], d=0, nd=0;
int divsize = 0;
int nondivsize = 0;
int arrsize = 0;
do{
printf("Enter a number within 1 and 5: ");
scanf("%d", &num);
if(num<1 || num>5)
printf("\nThe number you have entered is not within the given range.\n");
} while(num<1 || num>5);
printf("\nEnter ten numbers: \n");
for(int i=0; i<10; i++){
printf("Number %d: ", i+1);
scanf("%d", &arr[i]);
}
printf("\nEntered numbers: ");
for(int i=0; i<10; i++){
printf("%d ", arr[i]);
}
//calculates the arrays size of arr and displays it
for(int i; i<10; i++){
if(arr[i]!= 0)
arrsize++;
}
printf("\narrsize: %d\n", arrsize);
//Stores divisible and non-divisible inputs in to different arrays
for(int i=0; i<10; i++){
if(arr[i]%num == 0){
div[d] = arr[i];
d++;
}
else{
nondiv[nd] = arr[i];
nd++;
}
}
//calculates the number of elements in array div and displays it
for(int i=0; i<10; i++){
if(div[i] != 0){
divsize++;
}
}
printf("Number of divisible numbers: %d ", divsize);
printf("\nDivisible numbers: ");
for(int i=0; i<divsize; i++){
printf("%d ", div[i]);
}
}
I did some changes to your code and i think it works fine.....
in your code:
//calculates the number of elements in array div and displays it
for(int i=0; i<10; i++){
if(div[i] != 0){
divsize++;
}
}
i don't think this is necessary as you've already caluclated number of divisble and non divisble numbers and stored them in d and nd respectively (and) the numbers in the arrays div[] and nondiv[] in this loop:
for(int i=0; i<10; i++){
if(arr[i]%num == 0){
div[d] = arr[i];
d++;
}
else{
nondiv[nd] = arr[i];
nd++;
}
}
so while printing the number of divisible numbers and the divisible numbers array you can us d as parameter instead of divsize like this:
printf("Number of divisible numbers: %d ", d);//changed to d
printf("\nDivisible numbers: ");
for(int i=0; i<d; i++) //even here
{
printf("%d ", div[i]);
}
so to sum it all up, remove the last but one loop and change the
parameters of last loop from divsize to d in the last loop
*** and by the way I hope you declared size globally
so the code would be:
#include<stdio.h>
#define size 10 //i defined size globally here
int main()
{
int num, arr[size], div[size],nondiv[size],d=0, nd=0;
int arrsize = 0;
do{
printf("Enter a number within 1 and 5: ");
scanf("%d", &num);
if(num<1 || num>5)
printf("\nThe number you have entered is not within the given range.\n");
} while(num<1 || num>5);
printf("\nEnter ten numbers: \n");
for(int i=0; i<10; i++){
printf("Number %d: ", i+1);
scanf("%d", &arr[i]);
}
printf("\nEntered numbers: ");
for(int i=0; i<10; i++){
printf("%d ", arr[i]);
}
//calculates the arrays size of arr and displays it
for(int i=0; i<10; i++){
if(arr[i]!= 0)
arrsize++;
}
printf("\narrsize: %d\n", arrsize);
//Stores divisible and non-divisible inputs in to different arrays
for(int i=0; i<10; i++){
if(arr[i]%num == 0){
div[d] = arr[i];
d++;
}
else
{
nondiv[nd] = arr[i];
nd++;
}
}
printf("Number of divisible numbers: %d ", d);
printf("\nDivisible numbers: ");
for(int i=0; i<d; i++){
printf("%d ", div[i]);
}
}
-thank you
You declared a loop variable but didn't initialize it so its values is garbage value. Where you wrote
for(int i; i< size ;i++)
you should use i=0.
Other mistake is you can not declare size globally mean declare size as global variable and use as Loop variants means
for(int i=0;i< size;i++)
I also add commenting in your program where I change.
This loop isn't required, it makes the program too complex:
for(int i=0; i<10; i++){
if(div[i] != 0){
divsize++;
}
}
Because You already have counts of div array with the name of d.
Here is your updated Program
#include<stdio.h>
#include<conio.h>
#define size 10 // Every where you use hard coded 10 change it to size
int main(){
int num , arr[size], div[size], nondiv[size], d=0, nd=0;
int divsize = 0;
int nondivsize = 0;
int arrsize = 0;
do{
printf("Enter a number within 1 and 5: ");
scanf("%d", &num);
if(num<1 || num>5)
printf("\nThe number you have entered is not within the given range.\n");
} while(num<1 || num>5);
printf("\nEnter ten numbers: \n");
for(int i=0; i<10; i++){
printf("Number %d: ", i+1);
scanf("%d", &arr[i]);
}
printf("\nEntered numbers: ");
for(int i=0; i<10; i++){
printf("%d ", arr[i]);
}
//calculates the arrays size of arr and displays it
for(int i=0; i<10; i++){
// In loop variable you did'nt initialize i that's why it shows garbage value which is greater than 10000
if(arr[i]!= 0)
arrsize++;
}
printf("\narrsize: %d\n", arrsize);
//Stores divisible and non-divisible inputs in to different arrays
for(int i=0; i<10; i++){
if(arr[i]%num == 0){
div[d] = arr[i];
d++;
}
else{
nondiv[nd] = arr[i];
nd++;
}
}
//calculates the number of elements in array div and displays it
for(int i=0; i<10; i++){
if(div[i] != 0){
divsize++;
}
}
printf("Number of divisible numbers: %d ", d/*Here I just print d Because it is the count of divisible*/);
printf("\nDivisible numbers: ");
for(int i=0; i<d/*Here also used d */ ; i++){
printf("%d ", div[i]);
}
}

Resources