How To Program Array Loop Functions Call and Pointers In C++

Vu Old Solved Programming Based Assignments Codes are Available in this Section.
Post Reply
User avatar
ALI RAZA
★★★★★★★★★★★★★
★★★★★★★★★★★★★
Posts: 177
Joined: Fri Dec 22, 2017 8:10 am
Location: Lahore
Has thanked: 6 times
Been thanked: 7 times
Contact:
Status: Offline

How To Program Array Loop Functions Call and Pointers In C++

Post by ALI RAZA » Sat Sep 29, 2018 9:38 am

Write a program in C++ that will ask some programming related questions () from user and rank user according his / her answers in one of following categories:

1- Beginner level
2- Intermediate level
3- Advanced level

For this purpose, your program will ask three questions from the user (as given in the screenshots below), record their response in the form of true and false (as T or F) and store it in a character array using for loop.

You have to create a function named computeUserLevel () and pass that array to this function and receive the array as pointer within the function declaration. The function will determine and print the result that whether you are a beginner, intermediate or advance level user. If a user is not able to correctly answer any of the questions then user level will be set as beginner.


Note: Only use the following programming constructs in your program:

1. A Character Array
2. For Loop
3. Switch Statement
4. Function (Call by reference – receive array by using pointers)
5. If else (only allowed in function)
Answer:

Code: Select all

#include <iostream>
using namespace std;

void computeUserLevel(char *answerPtr){
    
     int check = 0;
     if (answerPtr[0] == 'F' || answerPtr[0] == 'f')
        check++;
     if (answerPtr[1] == 'T' || answerPtr[1] == 't')
        check++;
     if (answerPtr[2] == 'F' || answerPtr[2] == 'f')
        check++;
     
     switch(check){
     case 1:
          cout<< "Your level is Beginner\n";
          break;
     case 2:
          cout<< "Your level is Intermediate\n";
          break;
     case 3:
          cout<< "Your level is advanced\n";              
          break;
     default:
          cout<< "Your level is Beginner\n";
          break;
     }
}

main(){
       char answer[3];
       cout << "\t\t \"Program To Predict User Programming Level\" \n\n";
       
       for(int i=0; i<3; i++){
           switch(i){
           case 0:
                cout<< "Q: Switch is a Loop?\nA: ";
                cin>>answer[0];
                break;
           case 1:
                cout<< "Q: Pointer store memory address?\nA: ";
                cin>>answer[1];
                break;
           case 2:
                cout<< "Q: Semicolon after for loop is an error? \nA: ";
                cin>>answer[2];
                break;
           }
       }
    
       computeUserLevel(answer);
       system("pause");
}



Post Reply

Last 10 Members Who Visited This Topic

  • Similar Topics
    Replies
    Views
    Last post

Who is online

Users browsing this forum: No registered users and 1 guest