How to generate tables using arrays? - c

#include <stdio.h>
#include <stdlib.h>
int main()
{
system("color f0");
int k,i,j,n;
printf("Generate tables upto:");
scanf("%d",&n);
int tables[n][10];
printf("Table\t");
for(k=1;k<=10;k++)
{
printf("%dx\t",k);
}
printf("\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=10;j++)
{
tables[i][j]=i*j;
printf("%d\t",tables[i][j]);
}
}
return 0;
}
This is my code which i am working on but unfortunately I am not able to generate it the way I want.
The required output should look like this.

proposal fix for your code
C-arrays start at 0
you're missing tabs and linefeed at the proper locations
code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
system("color f0");
int k,i,j,n;
printf("Generate tables upto:");
scanf("%d",&n);
int tables[n][10];
printf("Table\t");
for(k=1;k<=10;k++)
{
printf("%dx\t",k);
}
printf("\n");
for(i=2;i<=n;i++)
{
printf("%d\t",i);
for(j=1;j<=10;j++)
{
tables[i-1][j-1]=i*j;
printf("%d\t",tables[i-1][j-1]);
}
printf("\n");
}
return 0;
}
display with n=4
Generate tables upto:4
Table 1x 2x 3x 4x 5x 6x 7x 8x 9x 10x
2 2 4 6 8 10 12 14 16 18 20
3 3 6 9 12 15 18 21 24 27 30
4 4 8 12 16 20 24 28 32 36 40

Array indexing starts from 0 and goes up to n-1. So you are accessing out of bounds which is undefined behaviour.
So you need to rewrite the loops as:
for(i=0; i < n; i++) {
for(j=0; j < 10; j++) {
tables[i][j] = (i+1)*(j+1);
printf("%d\t", tables[i][j]);
}
}

Related

pascal triangle in c without array [duplicate]

This question already has answers here:
Pascal's Triangle in C
(4 answers)
Closed 1 year ago.
I tried to code a program that will give the pascal triangle without using arrays and keeping in mind the formula that each element of the pascal triangle can be calculated as n choose k" and written like this:
n choose k = n! / k!(n-k)!
(for both n and k starting from 0)
so I also had to define the factorial function and it worked for the first 14 lines.
but in line 15 and more my numbers started to decrease and became also negative but I don't understand why this happened.
Here is the code:
#include <stdio.h>
int factorial(int a);
int main()
{
int row, j, i, space, tot;
scanf("%d", &row);
space=row;
for(i=0; i<row; i++)
{
for(space=0; space<row-i; space++)
{ printf(" "); }
for (j = 0; j <= i; j++)
{
if (j == 0 || i == 0)
{ tot=1; }
else
{
int n=factorial(i);
int k=factorial(j);
int z=factorial(i-j);
tot= n/(k*z);
}
printf("%6d", tot);
}
printf("\n");
}
}
int factorial(int a)
{
int fact=1;
for (int m=1; m<=a; m++)
{ fact*=m; }
return fact;
}
this is my output in line 15:
1 0 1 5 14 29 44 50 44 29 14 5 1 0 1
but the actual output should be this:
1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1
I couldn't find the problem so I would be happy and thankful if anyone could help me.
If you don't care much about computation time then you could compute coefficient directly from their recursive definition.
int pascal(int x,int y) {
if (x==0 || y==0) return 1;
return pascal(x-1,y)+pascal(x,y-1);}

Alternative way to Pascal's triangle using user defined functions

#include <stdio.h>
#include <conio.h>
int fact(int x);
int coeff(int y);
int main(){
int n,i,j,k;
printf("\nENTER THE POWER:");
scanf("%d",&n);
for (i=0;i<=n;i++){
for(k=n;k>i;k--){
printf(" ");
}
printf("%4d",coeff(i));
printf("\n");
}
getch();
}
int fact(int x){
int a , f=1 ;
for (a=x;a>0;a--){
if(x==0){
return 1;
}
return f*=a;
}
}
int coeff(int y){
int m ;
for(m=0;m<=y;m++) {
return (fact(y))/(fact(m)*fact(y-m));
}
}
I am a newbee in C programming . I have recently started printing patterns using C . I did the above coding of Pascal's Triangle myself . But my code doesn't give the correct answer in the CODE BLOCKS editor . I know that there are solutions to this in websites . But I want to develop my own reasonings(though it is oddly long ) . But I'm unable to find the fault here . Would someone kindly help me ?
THANKS ...
The solution below consists of correcting the errors in code along with some improvements:
1) Use getchar from stdio.h instead of getch from the non-standardconio.h.
2) The coeff function does not need a for loop.
3) The fact function calculates the factorial completely and only then returns the value.
4) Another for loop included in the main to print all the coefficients of a power in one line.
#include <stdio.h>
int fact(int x);
int coeff(int n, int y);
int main(){
int n,i,j,k;
n = i = j = k = 0;
printf("\nENTER THE POWER:");
scanf("%d",&n);
for (i=0;i<=n;i++){
for(k=n;k>i;k--){
printf(" ");
}
for(j=0;j<=i;j++){
printf("%4d",coeff(i, j));
}
printf("\n");
}
getchar();
}
int fact(int x){
int a , f=1 ;
if(x==0){
return 1;
}
for (a=x;a>0;a--){
f*=a;
}
return f;
}
int coeff(int n, int y){
return (fact(n))/(fact(y)*fact(n-y));
}
Output:
a.exe
ENTER THE POWER:5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
a.exe
ENTER THE POWER:6
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
a.exe
ENTER THE POWER:7
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1

conversion table from feet and inches to cm

I need to write a program which prints the conversion table from feet and inches to centimetres. The numbers printed in row i (counting from zero), column j (counting from zero) of the table should be the cm equivalent of i feet and j inches. i should go from 0 to 7, and j from 0 to 11. Each column should be five characters wide, and the cm figures should be rounded to the nearest integer.
The example of required output is given below:
0 3 5 8 10 13
30 33 36 38 41
61 64 66 69 71
91 94 97 99 102
The code I have prints only one row of inches and column of feet but I don't know how to make into table without producing lots of irrelevant repetitions.
The code is:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int i,j;
int cm,p;
for (i=0; i<= 11; i++) {
cm =round(i * 2.54);
printf ("%5d",cm);
}
for (j=0; j<=7; j++) {
p =round(j* 12.0 * 2.54);
printf ("%5d\n",p);
}
return 0;
}
This produces:
0 3 5 8 10 13 15 18 20 23 25 28 0
30
61
91
122
152
183
213
What am I doing wrong?
You have one loop after the other. What you need to do is run through the inches loop every iteration of your feet loop. What you get is nested loops:
#include <stdio.h>
int main()
{
for (int feet = 0; feet <= 7; ++feet) {
for (int inches = 0; inches < 12; ++inches) {
int microns = (feet * 12 + inches) * 25400;
int rounded_cm = (microns + 5000) / 10000;
printf("%5d", rounded_cm);
}
puts("");
}
}
I've made some other changes in my version; you're encouraged to study it and understand why it does what it does (read the man page for puts(), for example). Don't just copy it and hand it in - it will be obvious it isn't your code.
An alternative approach is to use a single loop (in inches), and insert a newline when we reach the 11th inch in each foot:
#include <stdio.h>
int main()
{
for (int i = 0; i < 96; ++i) {
printf("%4d%s",
(i * 25400 + 5000) / 10000,
i%12==11 ? "\n" : " ");
}
}
(You'll want to give meaningful names to your constants; the above is written in a "code-golf" style).
Whatever you do, don't be tempted to avoid multiplying by instead adding 2.54 repeatedly in the loop. Floating-point numbers are not exact, and addition will accumulate the error.
OP needs to put the "inches" loop inside the "foot" loop as well answered by others. #Toby Speight #VHS
Code could do its "round to nearest" via the printf() statement by using "%5.0f" to control the output width and rounding.
Let code use foot/inch instead of i/j #KevinDTimm for clarity.
#include <stdio.h>
#define INCH_PER_FOOT 12
#define CM_PER_INCH 2.54
int main(void) {
// go from 0 to 7, and ...
for (int foot = 0; foot <= 7; foot++) {
// from 0 to 11
// for (int inch = 0; inch < INCH_PER_FOOT; inch++) { is more idiomatic
for (int inch = 0; inch <= 11; inch++) {
printf("%5.0f", (foot * INCH_PER_FOOT + inch) * CM_PER_INCH);
}
puts("");
}
}
Output
0 3 5 8 10 13 15 18 20 23 25 28
...
213 216 218 221 224 226 229 231 234 236 239 241
You are running your loops backwards. First you need to run through feet and then through inches. But you are having it the other way round. Check the following snipped and compare it with your code and try to understand what's wrong.
#include <stdio.h>
#include <stdlib.h>
#include <math.h> // for rounding of a number
int main()
{
int i,j;
int cm,p;
for(i=0; i<=7;i++) {
for(j=0;j<=11;j++) {
cm = round(i*30.48 + j*2.54);
printf ("%5d",cm);
}
printf("\n");
}
return 0;
}

Printing numbers

I am writing a program to display as below
when n=3
1 2 3
7 8 9
4 5 6
when n=5
1 2 3 4 5
11 12 13 14 15
21 22 23 24 25
16 17 18 19 20
6 7 8 9 10
my program is
#include<stdio.h>
#include<conio.h>
int main()
{
int n=5,r=1,c=1,i=1,mid=0;
if(n%2==0)
mid=(n/2);
else
mid=(n/2)+1;
printf("mid = %d\n",mid);
while(r<=n)
{
while(c<=n)
{
printf("%d ",i);
c++;
i++;
}
r++;
if(r<=mid)
i=i+n;
else
i=i-(2*n);
printf("\n");
c=1;
}
getch();
return 0;
}
when I give n=3, I am getting my expected output. but when I give n=5 I am getting as below
1 2 3 4 5
11 12 13 14 15
21 22 23 24 25
16 17 18 19 20
11 12 13 14 15
Could someone please help how to achieve expected output.
Using you code the solution is
#include<stdio.h>
#include<conio.h>
int main()
{
int n=5,r=1,c=1,i=1,mid=0;
int maxRow = n;
if(n%2==0){
mid=(n/2);
maxRow--;
}
else
mid=(n/2)+1;
printf("mid = %d\n",mid);
while(r<=maxRow)
{
while(c<=n)
{
printf("%d ",i);
c++;
i++;
}
r++;
if(r<=mid)
i=i+n;
else if (r >= n)
i=n+1;
else
i=i-((1+(r-mid))*n);
printf("\n");
c=1;
}
getch();
return 0;
}
As you can see:
the i=i-(2*n); is changed. What you wrote wasn't generic, but specific for the n=3 case.
I added else if (r >= n).
Last thing you must use a specific variable for the outer while because of n must be decremented if n is even.
Some tips:
Give to your variables explanatory names
To make your code more readable declare variables 1 per line, if you want to init them.
Live empty lines between code chunks.
int main ()
{
int squareDim=5;
int row=1;
int col=1;
int valueToPrint=1;
int mid=0;
int maxRow = squareDim;
if(squareDim%2==0)
{
mid=(squareDim/2);
maxRow--;
}
else
{
mid=(squareDim/2)+1;
}
printf("mid = %d\n",mid);
while(row<=maxRow)
{
while(col<=squareDim)
{
printf("%d ",valueToPrint);
col++;
valueToPrint++;
}
row++;
if(row<=mid)
{
valueToPrint=valueToPrint+squareDim;
}
else if (row >= squareDim)
{
valueToPrint=squareDim+1;
}
else
{
valueToPrint=valueToPrint-((1+(row-mid))*squareDim);
}
printf("\n");
col=1;
}
return 0;
}

deletion of first two element of array declared statically not working

I am under a situation in a c that suppose i have code below:
#include <stdio.h>
main()
{
int size=8;
int data[18]={12,9,1,7,4,5,3,11};
int i,newS;
printf("check1 \n");
newS= size+1;
data[newS]=data[0] +data[1];
printf("news %d \n",data[newS]);
printf("check2 \n");
for(i=0;i<21;i++)
{
if (data[i] <data[newS] )
{
printf("check3 \n");
data[newS+1]= data[newS] +data[i] ;
newS++;
}
else
{
printf("check4 \n");
}
}
for(i=0;i<21;i++)
{
printf("%d ",data[i]);
}
printf("\n");
}
I expect it to produce result like this : 12 9 1 7 4 5 3 11 21 33 42 43 50 54 59 62 73 18 18 8. But i don't know why it has one "0" just after "11".How to remove this zero ?
The output obtained by corresponding code is this (which is not expected):
12 9 1 7 4 5 3 11 0 21 33 42 43 50 54 59 62 73 18 18 8
Here is the solution i have made to my problem of removing zero : (below is the code for future reference of any user).
#include <stdio.h>
main()
{
int size=8;
int data[18]={12,9,1,7,4,5,3,11};
int i,newS;
printf("check1 \n");
newS= size;
data[newS]=data[0] +data[1];
printf("news %d \n",data[newS]);
printf("check2 \n");
for(i=0;i<21;i++)
{
if (data[i] <data[newS] )
{
printf("check3 \n");
data[newS+1]= data[newS] +data[i] ;
//break;
newS++;
}
else
{
printf("check4 \n");
}
}
for(i=0;i<21;i++)
{
printf("%d ",data[i]);
}
printf("\n");
}
1) The topic of the question keeps changing. This is needlessly frustrating. If you have two questions, either express them both or break them into two separate posts.
2) Hint: What's the index of the last element of an 8-value array?
3) For delection -- restoring my original answer -- shift all the higher-indexed values downward one space in the array, and decrease the length by one.

Resources