C exercises and solutions: C array Pascal triangle


C array: Pascal triangle exercise

By using two-dimensional array, write C program to display a table that represents a Pascal triangle of any size. In Pascal triangle, the first and the second rows are set to 1. Each element of the triangle (from the third row downward) is the sum of the element directly above it and the element to the left of the element directly above it. See the example Pascal triangle(size=5) below:

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

 

Solution:  
#include<stdio.h>
#include<conio.h>
  void printPascalTr(int size);
int main()
    {

     int size;
     printf("Enter Pascal triangle size:");
     scanf("%d",&size);

     printPascalTr(size);
     getch();
     return 0;
 
    }
   
    void printPascalTr(int size){
         int PascalTr[size][size];
         int row,col;
         //assign zero to every array element
         for(row=0;row<size;row++)
            for(col=0;col<size;col++)  PascalTr[row][col]=0;
         //first and second rows are set to 1s  
         PascalTr[0][0]=1;
         PascalTr[1][0]=1;
         PascalTr[1][1]=1;
        
         for(row=2;row<size;row++){
              PascalTr[row][0]=1;
              for(col=1;col<=row;col++){
                        PascalTr[row][col]=PascalTr[row-1][col-1]+PascalTr[row-1][col];
                                        }
                            }
          //display the Pascal Triangle
          for(row=0;row<size;row++){
            for(col=0;col<=row;col++){               
                   printf("%d\t",PascalTr[row][col]);
                   }
            printf("\n");  
            }
        
         }


Comments

I have a C++ assignment (Need  comment

 I have a C++ assignment (Need

1. Write a C++ program that takes 10 integer values and stores it in an array called test_marks. Use separate functions to identify the following:
a) The largest test mark
b) The smallest test mark
c) Average test marks
d) The number of students who failed (less than 50 marks)
e) The number of students who scored A (more than 80 marks).
f) Print the following message accordingly
i) If number of students who failed is smaller than the number of students who scored A, print “Congratulations”
ii) If number of students who failed is larger than the number of students who scored A, print “Try Harder”
iii) If number of students who failed is equal to the number of students who scored A, print “Improvement Needed”

These are the questions


2017-06-10
sathya comment

 sathya

QUESTION 1: Write a C++ program to answer inquiries about student data.
The program will display a menu that enables the users to choose whether they want to view all students ’records or view only the records of a specific student by the student’s id. See sample below.
MENU

1. View all students’ records
2. View a student’s records by ID
3. Show the highest and the lowest final scores
Please enter your choice: 1

|StudentID | Quiz1 | Quiz2 | Mid-Term | Final | ==================================================
|1232 | 10 | 23 | 45 | 56 |
|2343 | 45 | 43 | 24 | 78 |
|2344 | 34 | 45 | 45 | 45 |
|3423 | 67 | 6 | 65 | 56 |

Note: These records will be stored in a 6X5 two-dimensional array. The user must provide input. All the input are integers.



2017-06-10
sathya comment

 sathya

QUESTION 1: Write a C++ program to answer inquiries about student data.
The program will display a menu that enables the users to choose whether they want to view all students ’records or view only the records of a specific student by the student’s id. See sample below.
MENU

1. View all students’ records
2. View a student’s records by ID
3. Show the highest and the lowest final scores
Please enter your choice: 1

|StudentID | Quiz1 | Quiz2 | Mid-Term | Final | ==================================================
|1232 | 10 | 23 | 45 | 56 |
|2343 | 45 | 43 | 24 | 78 |
|2344 | 34 | 45 | 45 | 45 |
|3423 | 67 | 6 | 65 | 56 |

Note: These records will be stored in a 6X5 two-dimensional array. The user must provide input. All the input are integers.



2017-06-10
akhil comment

 akhil

is this array is compact


2015-10-15
Mahmoud  comment

 Mahmoud

Thanks alot :)


2015-08-21
Ivonne comment

 Ivonne

Hi,
I cannot create PascalTr without a fix value in the brackets. What am I doing wrong?


2015-06-25
Dara comment

 Dara

This is to make sure that we are not adding blank values.


2014-03-24
abdallah asharf comment

 abdallah asharf

why we //assign zero to every array element
for(row=0;row<size;row++)
for(col=0;col<size;col++) PascalTr[row][col]=0;


2014-03-21



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.