Need help in C++ /** * * CIS 22A */ #include <iostream> using namespace std; boo
ID: 3802147 • Letter: N
Question
Need help in C++
/**
*
* CIS 22A
*/
#include <iostream>
using namespace std;
bool isLetter(char letter);
//Given a char, uses ASCII to determine if it is a letter
//Returns true if is a letter (a-z or A-Z)
//Returns false if it is not a letter
//isLetter('d') -> true
//isLetter('D') -> true
//isLetter('!') -> false
bool isNumber(char num);
//Given a char, uses ASCII to determine if it is a number
//Returns true if is a number (0-9)
//Returns false if it is not a number
//isNumber('a') -> false
//isNumber('3') -> true
void capitalize(char& letter);
//Given a char that is a valid letter (a-z or A-Z)
//Capitalizes the letter if it is a lower case,
//Leaves the letter unaltered if it is an upperCase
//capitalize('a') - > 'A'
//capitalize('A') -> 'A'
void spaceToComma(string& list);
//Given a string, alters any blank spaces in the string
//to be commas. Hint: Use a for loop, and string indexing
//spaceToComma("cats hats bats") -> "cats,hats,bats"
//spaceToComma("I love cookies! ") -> "I,love,cookies!,"
bool first10Last(int data[], int size);
//Given an array of ints, return true if 10 appears as
//either the first or last element in the array. The array will be size 1 or more.
//first10Last([1, 2, 10], 3) true
//first10Last([10, 1, 2, 3], 4) true
//first10Last([13, 10, 1, 2, 3], 5) false
bool equalFirstLast(int array[], int size);
//Given an array of ints, return true if the array is size 1 or more,
//AND the first element and the last element are equal.
//equalFirstLast([1, 2, 3], 3) false
//equalFirstLast([1, 2, 3, 1], 4) true
//equalFirstLast([1, 2, 1], 3) true
void printArray(int array[], int size);
//prints the contents of an array with a for loop
//see class notes for examples
void arrayPlus2(int array[], int size);
//Given an array of ints, add 2 to each element in the array
//return nothing. Remember arrays are automatically pass by reference
//arrayPlus2([1,2,3],3) -> [3, 4, 5]
//arrayPlus2([10, 30, 50, 79, 85], 5) -> [12, 32, 52, 81, 87]
//arrayPlus2([5], 1) -> [7]
void squareArray(int array[], int size);
//Given an array of ints, add multiplies each element in the array by itself
//return nothing. Remember arrays are automatically pass by reference
//squareArray([1,2,3],3) -> [1, 4, 9]
//squareArray([3, 5, 6, 8, 9], 5) -> [9, 25, 36, 64, 81]
//squareArray([5], 1) -> [25]
int main()
{
int result;
bool answer;
string value;
cout << "***Testing isLetter***"<< endl << endl;
answer = isLetter('d');
cout << "Should be true: " << answer << endl;
answer = isLetter('D');
cout << "Should be true: " << answer << endl;
answer = isLetter('!');
cout << "Should be false: " << false << endl;
cout << "***Testing isNumber***"<< endl << endl;
answer = isNumber('a');
cout << "Should be false: " << answer << endl;
answer = isNumber('9');
cout << "Should be true: " << answer << endl << endl;
cout << "***Testing capitalize***"<< endl << endl;
char lower = 'a';
capitalize(lower);
cout << "Should print A: " << lower << endl;
char upper = 'A';
capitalize(upper);
cout << "Should print A: " << upper << endl << endl;
cout << "***Testing spaceToComma***"<< endl << endl;
string list = "cats hats bats";
spaceToComma(list);
cout << "Should be cats,hats,bats: " << list << endl;
list = "I love cookies! ";
spaceToComma(list);
cout << "Should be I,love,cookies!,: " << list << endl << endl;
cout << "***Testing first10Last***"<< endl << endl;
int array1[] = {1, 2, 10};
const int SIZE1 = 3;
answer = first10Last(array1, SIZE1);
cout << "Should print true: " << answer << endl;
int array2[] = {10, 1, 2, 3};
const int SIZE2 = 4;
answer = first10Last(array2, SIZE2);
cout << "Should print true: " << answer << endl;
int array3[] = {13, 10, 1, 2, 3};
const int SIZE3 = 5;
answer = first10Last(array3, SIZE3);
cout << "Should print false: " << answer << endl << endl;
cout << "***Testing equalFirstLast***"<< endl << endl;
int array4[] = {1, 2, 3};
const int SIZE4 = 3;
answer = equalFirstLast(array4, SIZE4);
cout << "Should be false: " << answer << endl;
int array5[] = {10, 20, 50, 60, 80, 90, 10};
const int SIZE5 = 7;
answer = equalFirstLast(array5, SIZE5);
cout << "Should be true: " << answer << endl;
int array6[] = {1};
const int SIZE = 1;
answer = equalFirstLast(array6, SIZE);
cout << "Should be true: " << answer << endl << endl;
cout << "***Testing arrayPlus2***" << endl << endl;
int array7[] = {1, 2, 3};
const int SIZE7 = 3;
arrayPlus2(array7, SIZE7);
cout << "Should print 3 4 5: " << endl;
printArray(array7, SIZE7);
cout << endl;
int array8[] = {10, 30, 50, 79, 85};
const int SIZE8 = 5;
arrayPlus2(array8, SIZE8);
cout << "Should print 12 32 52 81 87: " << endl;
printArray(array8, SIZE8);
cout << endl;
int array9[] = {5};
const int SIZE9 = 1;
arrayPlus2(array9, SIZE9);
cout << "Should print 7: " << endl;
printArray(array9, SIZE9);
cout << endl << endl;
cout << "***Testing squareArray***" << endl << endl;
int array10[] = {1, 2, 3};
const int SIZE10 = 3;
squareArray(array10, SIZE10);
cout << "Should print 1 4 9: " << endl;
printArray(array10, SIZE10);
cout << endl;
int array11[] = {3, 5, 6, 8, 9};
const int SIZE11 = 5;
squareArray(array11, SIZE11);
cout << "Should print 9 25 36 64 81: " << endl;
printArray(array11, SIZE11);
cout << endl;
int array12[] = {5};
const int SIZE12 = 1;
squareArray(array12, SIZE12);
cout << "Should print 25: " << endl;
printArray(array12, SIZE12);
cout << endl << endl;
cout << "***End of Tests***" << endl;
return 0;
}
Explanation / Answer
Test.cpp
Compile: c++ Test.cpp -std=c++11
run: ./a.out
Code:
#include <iostream>
using namespace std;
bool isLetter(char letter);
bool isNumber(char num);
void capitalize(char& letter);
void spaceToComma(string& list);
bool first10Last(int data[], int size);
bool equalFirstLast(int array[], int size);
void printArray(int array[], int size);
void arrayPlus2(int array[], int size);
void squareArray(int array[], int size);
bool isLetter(char letter)
{
int c=letter;
if((c>=65 && c<=90) || (c>=97 && c<=122))
return true;
else
return false;
}
bool isNumber(char num)
{
int n=num;
if(n>=48 && n<=57)
return true;
else
return false;
}
void capitalize(char& letter)
{
int c=letter;
if(c>=97 && c<=122)
{
letter=letter-32;
}
}
void spaceToComma(string& list)
{
int c=0;
for( ;list[c]!='';c++)
{
if(list[c]==' ')
list[c]=',';
}
}
bool first10Last(int data[], int size)
{
if(data[0]==10)
return true;
else if(data[size-1]==10)
return true;
else
return false;
}
bool equalFirstLast(int array[], int size)
{
if(size>=1 && array[0]==array[size-1])
return true;
else
return false;
}
void printArray(int array[], int size)
{
cout<<" Array elements are: ";
for(int i=0;i<size;i++)
{
cout<<array[i]<<" ";
}
cout<<" ";
}
void arrayPlus2(int array[], int size)
{
for(int i=0;i<size;i++)
array[i]=array[i]+2;
}
void squareArray(int array[], int size)
{
for(int i=0;i<size;i++)
array[i]=array[i]*array[i];
}
int main()
{
int result;
bool answer;
string value;
cout << "***Testing isLetter***"<< endl << endl;
answer = isLetter('d');
cout << "Should be true: " << answer << endl;
answer = isLetter('D');
cout << "Should be true: " << answer << endl;
answer = isLetter('!');
cout << "Should be false: " << false << endl;
cout << "***Testing isNumber***"<< endl << endl;
answer = isNumber('a');
cout << "Should be false: " << answer << endl;
answer = isNumber('9');
cout << "Should be true: " << answer << endl << endl;
cout << "***Testing capitalize***"<< endl << endl;
char lower = 'a';
capitalize(lower);
cout << "Should print A: " << lower << endl;
char upper = 'A';
capitalize(upper);
cout << "Should print A: " << upper << endl << endl;
cout << "***Testing spaceToComma***"<< endl << endl;
string list = "cats hats bats";
spaceToComma(list);
cout << "Should be cats,hats,bats: " << list << endl;
list = "I love cookies! ";
spaceToComma(list);
cout << "Should be I,love,cookies!,: " << list << endl << endl;
cout << "***Testing first10Last***"<< endl << endl;
int array1[] = {1, 2, 10};
const int SIZE1 = 3;
answer = first10Last(array1, SIZE1);
cout << "Should print true: " << answer << endl;
int array2[] = {10, 1, 2, 3};
const int SIZE2 = 4;
answer = first10Last(array2, SIZE2);
cout << "Should print true: " << answer << endl;
int array3[] = {13, 10, 1, 2, 3};
const int SIZE3 = 5;
answer = first10Last(array3, SIZE3);
cout << "Should print false: " << answer << endl << endl;
cout << "***Testing equalFirstLast***"<< endl << endl;
int array4[] = {1, 2, 3};
const int SIZE4 = 3;
answer = equalFirstLast(array4, SIZE4);
cout << "Should be false: " << answer << endl;
int array5[] = {10, 20, 50, 60, 80, 90, 10};
const int SIZE5 = 7;
answer = equalFirstLast(array5, SIZE5);
cout << "Should be true: " << answer << endl;
int array6[] = {1};
const int SIZE = 1;
answer = equalFirstLast(array6, SIZE);
cout << "Should be true: " << answer << endl << endl;
cout << "***Testing arrayPlus2***" << endl << endl;
int array7[] = {1, 2, 3};
const int SIZE7 = 3;
arrayPlus2(array7, SIZE7);
cout << "Should print 3 4 5: " << endl;
printArray(array7, SIZE7);
cout << endl;
int array8[] = {10, 30, 50, 79, 85};
const int SIZE8 = 5;
arrayPlus2(array8, SIZE8);
cout << "Should print 12 32 52 81 87: " << endl;
printArray(array8, SIZE8);
cout << endl;
int array9[] = {5};
const int SIZE9 = 1;
arrayPlus2(array9, SIZE9);
cout << "Should print 7: " << endl;
printArray(array9, SIZE9);
cout << endl << endl;
cout << "***Testing squareArray***" << endl << endl;
int array10[] = {1, 2, 3};
const int SIZE10 = 3;
squareArray(array10, SIZE10);
cout << "Should print 1 4 9: " << endl;
printArray(array10, SIZE10);
cout << endl;
int array11[] = {3, 5, 6, 8, 9};
const int SIZE11 = 5;
squareArray(array11, SIZE11);
cout << "Should print 9 25 36 64 81: " << endl;
printArray(array11, SIZE11);
cout << endl;
int array12[] = {5};
const int SIZE12 = 1;
squareArray(array12, SIZE12);
cout << "Should print 25: " << endl;
printArray(array12, SIZE12);
cout << endl << endl;
cout << "***End of Tests***" << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.