Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Assignment No. 5 (CLO-3) : Programming Fundamentals 19-EE-116 Department

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

 Assignment No.

5 (CLO-3)
Subject: Programming Fundamentals (CS-1233)
Name: haroon- malik ; section: d; REG NO: 19-EE-116
Department: electrical engineering
Submitted to: dr. qamas gul
Date: 05-09-2020

Q. No. 1: Analyze and outline the following problem statement into the C++
program using Pointers.

a) Write a program that reads a group of numbers from the user and places them in an array of type float.
Once the numbers are stored in the array, the program should average them and print the result. Use
pointer notation wherever possible.

ANSWER:

// finds average of numbers typed by user

#include <iostream>

using namespace std;

int main()

float flarr[100]; //array for numbers

char ch; //user decision

int num = 0; //counts numbers input

do

cout << "Enter number: "; //get numbers from user

cin >> *(flarr+num++); //until user answers 'n'

cout << " Enter another (y/n)? ";


cin >> ch;

while(ch != 'n');

float total = 0.0; //total starts at 0

for(int k=0; k<num; k++) //add numbers to total

total += *(flarr+k);

float average = total / num; //find and display average

cout << "Average is " << average << endl;

return 0;

Output:

Source File:

assign 5.cpp

b) State whether the following are true or false. If false, explain why.
I) Two pointers that point to different arrays cannot be compared meaningfully.

II) Because the name of an array is a pointer to the first element of the array, array names can be
manipulated in precisely the same manner as pointers.
Answer:

I) True
II) False, an array name cannot be used to refer to another location in memory.

You might also like