Reputation: 131
here my code :
#include <stdio.h>
#define size 100
int main(){
int a,b,i,j;
int arr[size][size];
printf("input : ");
scanf("%d",&a);
int a[a];
arr[size][size]= a[a];
for(i=0;i<a ;i++){
a[i]=a[a]-a[i];
for(j=0; j<i; j++){
arr[i][j]=i+1/j+1;
}
}
printf("%d", arr[i][j]);
return 0;
}
I get problem and maybe someone want's to help me to fixed it. I confused in loop using decreasing and also array 2 dimension who not's print the true program.
this program should :
input :
5
output:
1.00 0.50 0.33 0.25 0.20
2.00 1.00 0.67 0.50
3.00 1.50 1.00
4.00 2.00
5.00
it's array using double or int?
Upvotes: 0
Views: 47
Reputation: 75062
a
after the integer a
cause collision.b
is not used.arr[size][size]
because it is out-of-range.double
should be used to store floating-point number. (float
may also work, but at least not int
)/
operator has higher precedence than +
operator, so you should use parenthesis to have it calculate +
before /
.Try this:
#include <stdio.h>
#define size 100
int main(){
int a,i,j;
double arr[size][size];
printf("input : ");
scanf("%d",&a);
for(i=0;i<a ;i++){
for(j=0; j<a-i; j++){
arr[i][j]=(double)(i+1)/(j+1);
}
}
for(i=0;i<a ;i++){
for(j=0; j<a-i; j++){
printf("%.2f%c", arr[i][j], j+1<a-i ? ' ' : '\n');
}
}
return 0;
}
Alternatively, you can avoid using floating-point number and use int
for the array.
#include <stdio.h>
#define size 100
int main(){
int a,i,j;
int arr[size][size];
printf("input : ");
scanf("%d",&a);
for(i=0;i<a ;i++){
for(j=0; j<a-i; j++){
arr[i][j]=(((i+1)*1000)/(j+1)+5)/10;
}
}
for(i=0;i<a ;i++){
for(j=0; j<a-i; j++){
printf("%d.%02d%c", arr[i][j]/100, arr[i][j]%100, j+1<a-i ? ' ' : '\n');
}
}
return 0;
}
The formula (((i+1)*1000)/(j+1)+5)/10
is doing:
1000
, so that it can calculate to 3 digit after the decimal point.5
and divide by 10
to round the result to 2 digits after the decimal point.Upvotes: 1