C :: Aufgabe #83 :: Lösung #1

1 Lösung Lösung öffentlich
#83

Erstellung Pascalsches Dreieck

Anfänger - C von BlackBird321 - 04.06.2015 um 22:43 Uhr
Schreibe ein Programm, welches das Pascalsches Dreieckwiedergibt.
Das Programm soll die Anzahl der Reihen entgegennehmen und dementsprechend widergeben (Nur halt ohne die Punkte :o)).

Beispiel für 5 Reihen:
....1
...11
..121
.1331
14641
#1
vote_ok
von devnull (8870 Punkte) - 06.06.2015 um 22:45 Uhr
Quellcode ausblenden C-Code
/*********************************
 * pascal.c   Pascalsches Dreieck
 *********************************/
#include <stdio.h>

#define MAXROWS 20

int get_rows(void) {
	int nrows = 0;
	
	while (nrows == 0) {	
		printf("Anzahl Reihen: "); 
		scanf("%d", &nrows); 
		if (nrows > MAXROWS) {
			printf("Anzahl Reihen darf maximal gleich %d sein.\n", MAXROWS);
			nrows = 0;
		}
	}
	return nrows;
}
	
void print_row(int *r, int *l, int n) {
	int k;

	printf("Reihe %2d: ", n);
	for (k=0; k < n; k++) {
		if (k == 0 || k == n-1) 
			r[k] = 1;
		else		
			r[k] = l[k-1] + l[k];
		printf(" %d ", r[k]);
	} 		
	printf("\n");
}

/* main */
int main( int argc, char **argv )
{
	int row_1[MAXROWS]; 
	int row_2[MAXROWS];
	int *this_row, *last_row, *temp_row;
	int crow, nrows = get_rows();

	row_1[0] = 1;
	last_row = row_1;
	this_row = row_2;
	for (crow=1; crow <= nrows; crow++) {
		print_row(this_row, last_row, crow);
		temp_row = last_row;
		last_row = this_row;
		this_row = temp_row;
	}
	return 0;
}

Kommentare:

Für diese Lösung gibt es noch keinen Kommentar

Bitte melden Sie sich an um eine Kommentar zu schreiben.
Kommentar schreiben