Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PROBLEM 2: (a) write an array declaration statement that stores the following va

ID: 3581972 • Letter: P

Question

PROBLEM 2: (a) write an array declaration statement that stores the following values in an array named volts: 40.21 -16.24, 18.98, 16.29, 19.54. Include these statements in a code fragment 24 that displays the values in the array in reverse order. [5 points] 15 points] (b) Consider the following function. Explain its operation. int countlets (string str) int i 0, strlength. 0, letters 0; str length str. length while (i strlength) if (isalpha (str[ij)) letters it return letters; (c) Why do you think successful application programs contain extensive data input validity checks? Explain two programming techniques for implementing such checks. [5 points]

Explanation / Answer

(a)

#include <iostream>
#include <cmath>
using namespace std;


int main()
{
   double volts[6] = {40.21,-16.24,18.98,24,16.29,-19.54};
  
   for(int i=5;i>=0;i--)
   {
       cout<<volts[i]<<" "; //reverse order array of volts
   }
   return 0;
}

output:

(b)

#include <iostream>
#include <cmath>
using namespace std;

int countlets(string str)
{
   int i=0, strlength=0, letters=0;
   strlength =str.length();
   while(i < strlength)
   {
       if(isalpha(str[i]))
       letters++;
       i++;
   }
   return letters;
  
}

int main()
{
   cout<<countlets("Computer");
   return 0;
}

output:

8

The function is counting the number of letters in the string passed as argument.

(c)

Successful Application programs need input validity checks to make sure that the program operates on clean,useful and correct data. The programming techniques for implementing such checks are :

1. if statements

2.cin.fail() returns true if the last cin failed, and false otherwise.

3. cin.eof() is used to check end of file errors. It returns 1 if the program tried reading from the end of the file.

(d) and (e)

#include <iostream>
#include <cmath>
using namespace std;

class Date
{
   private:
   int day;
   int month;
   int year;
   int dateValue;
   public:
   Date() //prototype for Date constructor
   {
      
   }
   void setDate(int day,int month,int year) //set method to set values of day,month and year
   {
       this->day = day;
       this->month = month;
       this->year = year;
   }
   void showDate() //method to display date
   {
       cout<<day<<"/"<<month<<"/"<<year;
   }
  
};

int main()
{
   Date d;
   d.setDate(16,12,2016);
   cout<<"Today is ";
   d.showDate();
   return 0;
}

output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote