Note: None of these functions should use cout. It is OK to use cout while
debugging to check how your function is working, but your final submission
should not contain cout in any function but main.
Head
====
Name the source file for this section head.cpp.
Write a function named "head" which takes an array of integers as an
argument,
and returns the first element in the array.
Write a function named main which outputs the result of testing your
function
with 5 different inputs. Here is an example of a main function which
tests a
head function::
int main()
{
int a[] = {1, 2, 3, 4, 5};
int b[] = {45, 67};
int c[] = {17, 34 37};
int d[] = {23};
int e[] = {90};
cout << head(a) == 1 << endl;
cout << head(b) == 45 << endl;
cout << head(c) == 17 << endl;
cout << head(d) == 23 << endl;
cout << head(e) == 90 << endl;
}
If you use this main, you'll be able to compile your program and run it to
see
if your head function works.
Sum
===
Name the source file for this section sum.cpp.
Write a function named "sum" which takes as arguments an array of int and
an int representing the size of the array. The function should return the
sum
of all the numbers in the array.
Write a function named main which outputs the result of testing your
function
with 5 different inputs.
And
===
Name the source file for this section and.cpp.
Write a function named "and_" which takes as arguments an array of bool
and
an integer representing the size of the array. The function should return
true
if every element in the array is true, otherwise it should return false.
Write a function named main which outputs the result of testing your
function
with 5 different inputs.
Or
==
Name the source file for this section or.cpp.
Write a function named "or_" which takes as arguments an array of bool and
an integer representing the size of the array. The function should return
true
if *any* element in the array is true, otherwise it should return false.
Write a function named main which outputs the result of testing your
function
with 5 different inputs.
Max
===
Name the source file for this section max.cpp.
Write a function named "max" which takes as arguments an array of int and
an integer representing the size of the array. The function should return
the
int in the array which is the largest of all the elements in the array.
Write a function named main which outputs the result of testing your
function
with 5 different inputs.
lookup
======
Name the source file for this section lookup.cpp.
Write a function named "lookup" which takes as arguments an array of int,
an integer representing the size of the array, and an int to search for in
the
array. The function should search the given array for the given int and
return
the *index* of the int in the array if found, otherwise it should return -
1.
In the same source file, write a function named "isElem" which takes the
same
arguments as lookup, but returns true if the int to be search for is in
the
array, and false otherwise. I would recommend calling lookup inside of
isElem
to implement this function; isElem should be one line long.
Write a function named main which outputs the result of testing both your
functions with 5 different inputs each.
Here are some examples that should work if you write the lookup function
correctly::
const int SIZE = 6;
int a[SIZE] = {7, 3, 6, 1, 2, 9};
lookup(a, SIZE, 3) == 1; // Because 3 is in the array @ index 1.
lookup(a, SIZE, 9) == 5; // Because 9 is in the array @ index 5.
lookup(a, SIZE, 7) == 0; // Because 7 is in the array @ index 0.
lookup(a, SIZE, 8) == -1; // Because 8 is not in the array.
lookup(a, SIZE, 0) == -1; // Because 0 is not in the array.
lookup(a, SIZE, 182) == -1; // Because 182 is not in the array.
isElem(a, SIZE, 3) == true; // Because 3 is in the array
isElem(a, SIZE, 9) == true; // Because 9 is in the array
isElem(a, SIZE, 7) == true; // Because 7 is in the array
isElem(a, SIZE, 8) == false; // Because 8 is not in the array.
isElem(a, SIZE, 0) == false; // Because 0 is not in the array.
isElem(a, SIZE, 182) == false; // Because 182 is not in the array.
Unlines
=======
Name the source file for this section unlines.cpp.
Write a function named "unlines" which takes as arguments an array of
string
and an integer representing the size of the array. The function should
return
a string which is the combination of all the strings in the array
concatenated
together with a newline after each string. For example, this code should
output 1/true if you write unlines correctly::
const int SIZE = 3;
string foo[SIZE] = {"foo", "bar", "baz"};
cout << (unlines(foo, SIZE) == "foo\nbar\nbaz\n") << endl;
Write a function named main which outputs the result of testing your
function
with 5 different inputs.
Add 3
=====
Name the source file for this section add3.cpp.
Write a function named "add3" which takes as arguments an array of
integers and
an integer representing the size of the array. The function should not
return
anything, i.e. it should have a return type of void. The function should
change the contents of the array argument it received so that each element
has
3 added to it.
Write a function named main which outputs the result of testing your
function
with 5 different inputs. An example main::
int main()
{
const int SIZE = 3;
int a[SIZE] = {1, 2, 3};
int b[SIZE] = {4, 5, 6};
int c[SIZE] = {3, 3, 3};
int d[SIZE] = {-1, 0, 1};
int e[SIZE] = {99, 32, 49};
add3(a, SIZE);
add3(b, SIZE);
add3(c, SIZE);
add3(d, SIZE);
add3(e, SIZE);
cout a[0] == 4 && a[1] == 5 && a[2] == 6 << endl;
cout b[0] == 7 && b[1] == 8 && b[2] == 9 << endl;
cout c[0] == 6 && c[1] == 6 && c[2] == 6 << endl;
cout d[0] == 2 && d[1] == 3 && d[2] == 4 << endl;
cout e[0] == 102 && e[1] == 35 && e[2] == 52 << endl;
}
Init
====
Name the source file for this section init.cpp.
Write a function named "init" which takes as arguments an array of
integers, an
integer representing the size of the array, and an int which is the value
we
want to "initialize" every element of the array to be. The function
should set
every element of the array equal to the int given. This function is
useful
for the common task of initializing elements in a large array so we start
from
a known good state, instead of staring with an array full of random
garbage.
Write a function named main which outputs the result of testing your
function
with 5 different inputs.
Reverse
=======
Name the source file for this section reverse.cpp.
Write a function named "reverse" which takes as arguments an array of
integers
and an integer representing the size of the array. The function should
reverse
the contents of the array.
Write a function named main which outputs the result of testing your
function
with 5 different inputs.
Note: reverse should not use cout at all. The point of the function is to
change the array it is given, not simply loop through the array backwards
sending the contents to the console screen.
Hint: Don't start coding this until you understand what you want to do on
paper. Draw some arrays. Draw some arrows. Draw some boxes (variables).
Think about the process you go through with paper and pencil to create a
new
array with the contents of the first but reversed. Another way to
approach it
is to use your eraser and some boxes, modifying the first array you drew
one
step at a time until the contents are reversed.
1.
//head.cpp
#include <iostream>
using namespace std;
int head(int arr[]);
int main() {
int a[] = {1, 2, 3, 4, 5};
int b[] = {45, 67};
int c[] = {17, 34 ,37};
int d[] = {23};
int e[] = {90};
// testing of head
cout << head(a) << endl;
cout << head(b) << endl;
cout << head(c) << endl;
cout << head(d) << endl;
cout << head(e) << endl;
return 0;
}
int head(int arr[])
{
return arr[0];
}
//end of head.cpp
Output:
2.
//sum.cpp
#include <iostream>
using namespace std;
int sum(int arr[],int size);
int main() {
int a[] = {1, 2, 3, 4, 5};
int b[] = {45, 67};
int c[] = {17, 34 ,37};
int d[] = {23};
int e[] = {90};
// testing of sum
cout << sum(a,5) << endl;
cout << sum(b,2) << endl;
cout << sum(c,3) << endl;
cout << sum(d,1) << endl;
cout << sum(e,1) << endl;*/
return 0;
}
int sum(int arr[],int size)
{
int total = 0;
for(int i=0;i<size;i++)
total += arr[i];
return total;
}
//end of sum.cpp
Output:
3.
//and.cpp
#include <iostream>
using namespace std;
bool and_(bool arr[],int size);
int main() {
bool a_bool[] = {true,false,false,true,true};
bool b_bool[] = {true};
bool c_bool[] = {false};
bool d_bool[] = {true,true,true,true,false};
bool e_bool[] = {true,true,true,true,true};
// testing of and_
cout << and_(a_bool,5) << endl;
cout << and_(b_bool,1) << endl;
cout << and_(c_bool,1) << endl;
cout << and_(d_bool,5) << endl;
cout << and_(e_bool,5) << endl;
return 0;
}
bool and_(bool arr[],int size)
{
for(int i=0;i<size;i++)
if(!arr[i])
return false;
return true;
}
//end of and.cpp
Output:
4.
//or.cpp
#include <iostream>
using namespace std;
bool or_(bool arr[],int size);
int main() {
bool a_bool[] = {true,false,false,true,true};
bool b_bool[] = {true};
bool c_bool[] = {false};
bool d_bool[] = {true,true,true,true,false};
bool e_bool[] = {true,true,true,true,true};
// testing of or_
cout << or_(a_bool,5) << endl;
cout << or_(b_bool,1) << endl;
cout << or_(c_bool,1) << endl;
cout << or_(d_bool,5) << endl;
cout << or_(e_bool,5) << endl;
return 0;
}
bool or_(bool arr[],int size)
{
for(int i=0;i<size;i++)
if(arr[i])
return true;
return false;
}
//end of or.cpp
Output:
Note: None of these functions should use cout. It is OK to use cout while debugging...
c++ program8. Array/File Functions Write a function named arrayToFile. The function should accept three arguments: the name of a file, a pointer to an int array, and the size of the array. The function should open the specified file in binary mode, write the contents of the array to the file, and then close the file. Write another function named fileToArray. This function should accept three argu- ments: the name of a file, a pointer to an int array, and the size...
C++ problem with dynamic arrays is that once the array is created using the new operator the size cannot be changed. For example, you might want to add or delete entries from the array similar to the behavior of a vector. This project asks you to create a class called DynamicStringArray that includes member functions that allow it to emulate the behavior of a vector of strings. The class should have: A private member variable called dynamicArray that references a...
DO NOT use for loop and while loop Goal: Learn how to use recursive functions. General requirement: Use cout to fully demonstrate your works for all tasks. The output must clearly show how each of these functions work. If no output is displayed for a task below, 0 point will be given to the task. If the output is not clear, some points may be deducted. Task 1 – Reverse array (3 pts) Given an integer array like: const...
#include <iostream> using namespace std; int * newZeroArray(int size) { //your code here } int main() { int size = 10; int * A = newZeroArray(size); for(int i = 0; i < size; i++) cout << A[i] << " "; cout << endl; }Write a function that takes a size, creates a new array of that size with all zeros, and returns the array. If the size is not a valid size for an array, do not return a valid...
Write a C++ program that contains 2 functions. LastLargestIndex, that takes as paraneters an int array and // its size and returns the index of the first occurrence of the largest element II in the array. Also, write a function to display the array #include ·peh.h" #include <iostream> using namespace std const int ARRAY_SIZE = 15; int main int list[ARRAY SIZE56, 34, 67, 54, 23, 87, 66, 92. 15, 32, 5, 54, 88, 92, 30 cout < List elements: "...
Homework Question Write a void function called transformArray that takes two parameters - a reference to a pointer to a dynamically allocated array of ints, and the size of that array. The pointer is passed by referencebecause you want to change the value of the pointer. The function should dynamically allocate an array that is twice as long, filled with the values from the original array followed by each of those values times 2. For example, if the array that was...
/* Array expander: Write a function that accepts an int array as argument. The function should create a new array that is n times the size of the argument array, where n is a user input. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function should return a pointer to the new array */ #include <iostream> using namespace std; const int NUM_ELEM...
may i ask for help with this c++ problem? this is the code i have for assignment 4 question 2: #include<iostream> #include<string> #include<sstream> #include<stack> using namespace std; int main() { string inputStr; stack <int> numberStack; cout<<"Enter your expression::"; getline(cin,inputStr); int len=inputStr.length(); stringstream inputStream(inputStr); string word; int val,num1,num2; while (inputStream >> word) { //cout << word << endl; if(word[0] != '+'&& word[0] != '-' && word[0] != '*') { val=stoi(word); numberStack.push(val); // cout<<"Val:"<<val<<endl; } else if(word[0]=='+') { num1=numberStack.top(); numberStack.pop(); num2=numberStack.top(); numberStack.pop();...
Write a C++ program In this assignment you will complete the definition of two functions that implement the repeated squaring algorithm described in the Stamp textbook on pages 98-99. Note that your implementation should not use the pow() function, the powl() functions, or any other built-in exponentiation functions. program4-driver.cpp - This file contains a completed main() function that serves as the driver to parse the command line, pass the values to the powerModN() function, and print the result. Make no...