C programozás help
-
#220
Ha ilyesmi kimenetre gondolsz:
0. oszlop: 10
1. oszlop: 10
2. oszlop: 1
3. oszlop: 5
4. oszlop: 5
5. oszlop: 1
6. oszlop: 10
7. oszlop: 10
xx....xx
xx....xx
xx....xx
xx....xx
xx....xx
xx.xx.xx
xx.xx.xx
xx.xx.xx
xx.xx.xx
xxxxxxxx
Akkor ilyesmi kell:
#include <stdio.h>
#define N 8 //Oszlopok száma
#define M 10 //Sorok száma
int main(){
int i, j, max, sza[M];
char tabla[M][N];
//Inicializalas
for (i=0; i<M; i++)
for(j=0 ;j<N; j++)
tabla[i][j] = '.';
printf("Kerem a part adatait:\n");
//Magasságok bekérése
for (j=0; j<N; j++) {
printf ("%d. oszlop: ",j);
scanf("%d", &sza[j]);
//Túlcsordulás megakadályozása
while ((sza[j]<0) || (sza[j]>M)) {
printf ("%d. oszlop: ",j);
scanf("%d", &sza[j]);
}
}
//Berajzolás
for (j=0; j<N; j++)
for(i=(M-sza[j]); i<M; i++)
tabla[i][j]='x';
//Kiiras
for (i=0; i<M; i++) {
for (j=0; j<N; j++) {
printf("%c", tabla[i][j]);
}
printf("\n");
}
return 0;
}
Sőt egy kicsit optimalizálva:
#include <stdio.h>
#define N 8 //Oszlopok száma
#define M 10 //Sorok száma
int main(){
int i, j, max, sza[M];
char tabla[M][N];
printf("Kerem a part adatait:\n");
//Magasságok bekérése
for (j=0; j<N; j++) {
printf ("%d. oszlop: ",j);
scanf("%d", &sza[j]);
//Túlcsordulás megakadályozása
while ((sza[j]<0) || (sza[j]>M)) {
printf ("%d. oszlop: ",j);
scanf("%d", &sza[j]);
}
}
//Kirajzolás
for (i=0; i<M; i++) {
for (j=0; j<N; j++) {
if ((i>=(M-sza[j])) && (i<M)) {
tabla[i][j]='x';
} else {
tabla[i][j]='.';
}
printf("%c", tabla[i][j]);
}
printf("\n");
}
return 0;
}