SlideShare a Scribd company logo
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at :- info@cpphomeworkhelp.com or
reach us at :- https://www.cpphomeworkhelp.com/
1. Write a function named "location_of_largest" that takes as its arguments the following:
(1) an array of integer values;
(2) an integer that tells how many integer values are in the array.
The function should return as its value the subscript of the cell containing the largest of the
values in the array.
Thus, for example, if the array that's passed to the function looks like this:
then the function should return the integer 2 as its value. If there is more than one cell
containing the largest of the values in the array, then the function should return the smallest
of the subscripts of the cells containing the largest values. For example, if the array that's
passed to the function is
hen the largest value occurs in cells 2 and 5 , so the function should return the integer
value 2 .
2. Write a function named "location_of_target" that takes as its arguments the following:
(1) an array of integer values;
(2) an integer that tells how many integer values are in the array;
cpphomeworkhelp.com
(3) an integer "target value".
The function should determine whether the given target value occurs in any of the
cells of the array, and if it does, the function should return the subscript of the cell
containing the target value. If more than one of the cells contains the target value,
then the function should return the largest subscript of the cells that contain the target
value. If the target value does not occur in any of the cells, then the function should
return the sentinel value −1 . Thus, for example, if the target value that's passed to the
function is 34 and the array that's passed to the function looks like this:
then the target value occurs in cells 3 and 5 , so the function should return the integer
value 5 .
3. Write a function named "rotate_right" that takes as its arguments the following:
(1) an array of floating point values;
(2) an integer that tells the number of cells in the array;
The function should shift the contents of each cell one place to the right, except for the
contents of the last cell, which should be moved into the cell with subscript 0 . Thus,
for example, if the array passed to the function looks like this:
cpphomeworkhelp.com
then when the function returns, the array will have been changed so that it looks
like this:
The function should not return a value.
4. Write a function named "shift_right" that takes as its arguments the following:
(1) an array of floating point values;
(2) an integer, call it "left", that tells the leftmost cell of the part of the array to be
shifted;
(3) an integer, call it "right", that tells the rightmost cell of the part of the array to be
shifted;
(4) a positive integer, call it "distance" that tells how many cells to shift by.
The function should make sure that left is less than or equal to right, and that
distance
is greater than zero. If either of these conditions fails, the function should return
the
value 1 to indicate an error. Otherwise it should shift by distance cells the
contents of
the array cells with subscripts running from left to right . Thus, for example, if
the array
passed to the function looks like this:
cpphomeworkhelp.com
so that when the function returns, the array will have been changed so that it
looks like
this:
The question marks in cells 3 and 4 indicate that we don't care what numbers
are in
those cells when the function returns. Note that the contents of cells 8 and 9
have
changed, but the contents of cell 10 is unchanged. The function need not take
any
precautions against the possibility that the cells will be shifted beyond the end of
the
array (the calling function should be careful not to let that happen).
5. Write a function named "subtotal" takes as its arguments the following:
(1) an array of floating point values;
(2) an integer that tells the number of cells in the array.
The function should replace the contents of each cell with the sum of the contents
of all
the cells in the original array from the left end to the cell in question. Thus, for
example,
if the array passed to the function looks like this:
because 5.8 + 2.6 = 8.4 and 5.8 + 2.6 + 9.1 = 17.5 and so on. Note that the
contents of cell 0 are not changed. The function should not return a value.
cpphomeworkhelp.com
1. Here is the simplest solution.
/************** L O C A T I O N O F L A R G E S T **************
DESCRIPTION: Finds the index of the largest number in an array.
PARAMETERS:
a An array of integers to be scanned.
n The number of objects in the array, starting at cell 0.
RETURNS: The index of the cell containing the largest integer.
If the largest integer appears in more than one cell,
then the index of the leftmost cell where it appears
will be returned.
ALGORITHM: A variable that will hold the index of the largest
integer is initialized to zero, and we pretend to have
scanned . that cell and found that it contains the largest
integer seen "so far". Then successive cells are examined
and compared with the cell containing the largest integer
so far. When a cell containing a new largest integer
is encountered, the variable that holds the index of the
largest integer seen so far is modified to the new
index
AUTHOR: W. Knigh
*******************************************************************
/
cpphomeworkhelp.com
int location_of_largest (const int a[], int n)
{
int best = 0; // Location of the largest so far.
int i;
for (i = 1; i < n; ++i) // Start comparing at the second cell.
if (a[i] > a[best])
best = i;
return best;
}
The following solution is not as elegant as the one on the preceding page.
/************** L O C A T I O N O F L A R G E S T
**************
DESCRIPTION: Finds the index of the largest number in an array.
PARAMETERS:
a An array of integers to be scanned.
n The number of objects in the array, starting at cell 0.
RETURNS: The index of the cell containing the largest integer.
If the largest integer appears in more than one cell,
then the index of the leftmost cell where it appears
will be returned.
cpphomeworkhelp.com
ALGORITHM: A variable that will hold the index of the largest
integer is initialized to zero, and we pretend to have
scanned that cell and found that it contains the largest
integer seen "so far". We initialize a "largest_value"
variable to the value a[0]. Then successive cells are examined
and compared with the value of the largest integer so
far. When a cell containing a new largest integer is
encountered, the variable that holds the index of the
largest integer seen so far is modified to the new
index, and the largest_value variable is updated.
AUTHOR: W. Knight
*******************************************************************
/
int location_of_largest (const int a[], int n)
{
int largest_value = a[0]; // First cell contains largest so far.
int best = 0; // Location of the largest so far.
int i;
for (i = 1; i < n; ++i)
if (a[i] > largest_value)
{
largest_value = a[i];
best = i;
cpphomeworkhelp.com
}
return best;
}
The following is NOT an acceptable solution because it does not allow for the
possibility that all the integers in the array may be negative or zero.
int location_of_largest (const int a[], int n) // NOT ACCEPTABLE
{
int largest_value = 0; // Assume the largest value will be > 0.
int best; // Eventual location of the largest value.
int i;
for (i = 0; i < n; ++i)
if (a[i] > largest_value) // Will be true for some i only if
{
// the array contains at least one
largest_value = a[i]; // value strictly greater than 0.
best = i;
}
return best;
}
The incorrect solution above can be corrected by initializing largest_value to
the most negative possible integer value (INT_MIN, which is defined in the
header file <limits.h>) and initializing best to 0 .
cpphomeworkhelp.com
2. Here is the simplest solution. It searches from right to left and quits when (if)
if finds the target.
/************** L O C A T I O N O F T A R G E T **************
DESCRIPTION: Finds the index of the cell (if any) where a "target“
integer is stored.
PARAMETERS:
a An array of integers to be scanned.
n The number of objects in the array, starting at cell 0.
target The integer that we hope to find in some cell of array a.
RETURNS: The index of the cell containing the integer "target"
provided there is such a cell. If there is not, then
the function returns -1 as a sentinel value.
If the target integer appears in more than one cell,
then the index of the rightmost cell where it appears
will be returned.
ALGORITHM: The value parameter "n" is used to scan backward across
the array, looking for a cell containing a copy of
"target". If a copy is found, the search is halted
and the index of that cell is returned. If no such cell
is found, "n" will run off the left end of the array and
wind up with value -1.
AUTHOR: W. Knight
***************************************************************
cpphomeworkhelp.com
/
int location_of_target (const int a[], int n, int target)
{
--n; // Make n "point" to the last cell of the array.
while (n >= 0 && a[n] != target) // Search right to left --n;
return n; // Returns -1 if target is not in array a[].
}
The following solution is acceptable, although it is not in general as efficient as
the one above because it always examines every cell of the array.
/************** L O C A T I O N O F T A R G E T **************
DESCRIPTION: Finds the index of the cell (if any) where a "target“
integer is stored.
PARAMETERS:
a An array of integers to be scanned.
n The number of objects in the array, starting at cell 0.
target The integer that we hope to find in some cell of array a.
RETURNS: The index of the cell containing the integer target,
provided there is such a cell. If there is not, then
the function returns -1 as a sentinel value.
If the target integer appears in more than one cell,
then the index of the rightmost cell where it appears
will be returned.
cpphomeworkhelp.com
ALGORITHM: A location variable is initialized to -1 in case no copy
of "target" is found. Then each cell of the array is
examined, starting at the left end, and each time a
copy "target" is found, the location variable is updated
to the index of that cell.
AUTHOR: W. Knight
*******************************************************************
/
int location_of_target (const int a[], int n, int target)
{
int location = -1; // Target not seen yet.
int i;
for (i = 0; i < n; ++i)
if (a[i] == target)
location = i;
return location;
}
cpphomeworkhelp.com
3. /********************* R O T A T E R I G H T
*********************
DESCRIPTION: Shifts the contents of array cells one cell to the right,
with the last cell's contents moved to the left end.
PARAMETERS:
a The floating point array to be modified.
n The number of objects in the array, starting at cell 0.
RETURNS: Void (no value).
ALGORITHM: The object in the right-most cell is copied to a temporary
location, and then the object each cell to the left
of the last cell is copied to its immediate right
neighbor; the process moves from right to left. Finally, the
object in the temporary location is copied to the leftmost
cell.
AUTHOR: W. Knight
*******************************************************************
/
void rotate_right (float a[], int n)
{
float temp = a[n-1]; // Hold the contents of the last cell.
int i;
for (i = n - 1; i >= 1; --i)
a[i] = a[i-1]; a[0] = temp; }
cpphomeworkhelp.com
4. /*********************** S H I F T R I G H T ********************* DESCRIPTION:
Shifts the contents of some subarray in an array to the
right by a specified number of cells.
PARAMETERS:
a The floating point array to be modified.
left The index of the leftmost cell of the subarray to be
shifted.
right The index of the rightmost cell of the subarray.
distance The number of cells by which the subarray will be shifted.
RETURNS: 1 provided left <= right and distance > 0; returns 0
if either of these conditions is violated.
ALGORITHM: An index variable is initialized to "point" to the
rightmost cell of the subarray to be shifted;
another index variable is initialized to point to the cell to
which the rightmost object will be copied. Then the
copying takes place and the indexes are moved to the
left (decremented by 1). The occurs repeatedly until
the object in the leftmost cell of the subarray has
been copied.
AUTHOR: W. Knight
*******************************************************************
/
cpphomeworkhelp.com
int shift_right (float a[], int left, int right, int distance)
{
if (left > right || distance <= 0)
return 1; int i = right, // points to the cell to be shifted
j = i + distance; // points to the receiving cell
while (i >= left)
{
a[j] = a[i];
--i;
--j;
}
return 0;
}
Here is a slightly more elegant version.
/*********************** S H I F T R I G H T
*********************
DESCRIPTION: Shifts the contents of some subarray in an array to the
right by a specified number of cells.
PARAMETERS:
a The floating point array to be modified.
left The index of the leftmost cell of the subarray to be
shifted.
cpphomeworkhelp.com
right The index of the rightmost cell of the subarray.
distance The number of cells by which the subarray will be
shifted.
RETURNS: 1 provided left <= right and distance > 0; returns 0
if either of these conditions is violated.
ALGORITHM: An index variable is initialized to "point" to the
rightmost cell of the subarray to be shifted. Then
the object in that cell is copied to the cell that's
"distance" units to the right. Then the index is
decremented by 1 and the same process is repeated.
This continues until all objects in the subarray have
been copied.
AUTHOR: W. Knight
*******************************************************************
/
int shift_right (float a[], int left, int right, int distance)
{
if (left > right || distance <= 0)
return 1;
int i;
for (i = right; i >= left; --i)
a[i + distance] = a[i];
return 0;}
cpphomeworkhelp.com
5. /************************* S U B T O T A L ***********************
DESCRIPTION: Replaces each number in an array with the sum of all the
numbers up to that location in the original array.
PARAMETERS:
a The floating point array to be modified.
n The number of objects in the array, starting at cell 0.
RETURNS: Void (no value).
ALGORITHM: Starting with cell 1 and moving right, the number in each
cell is replaced by the sum of that number and the
sum that's now in the cell just to the left.
AUTHOR: W. Knight
*******************************************************************
/
void subtotal (float a[], int n)
{
int i;
for (i = 1; i < n; ++i)
a[i] += a[i-1];
cpphomeworkhelp.com

More Related Content

DOCX
611+tutorial
DOCX
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
DOCX
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
PDF
Data structures arrays
DOCX
Please Please Please Read the instructions and do everything li.docx
PPTX
enhancement of sorting algorithm
PPTX
Arrays in programming
PPTX
Basic Sorting algorithms csharp
611+tutorial
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Data structures arrays
Please Please Please Read the instructions and do everything li.docx
enhancement of sorting algorithm
Arrays in programming
Basic Sorting algorithms csharp

Similar to CPP Programming Homework Help (20)

PDF
computer notes - List implementation
PDF
Linear Data Structures_SSD.pdf
PPTX
data structures and algorithms Unit 3
PPT
Advanced s and s algorithm.ppt
PDF
Amcat automata questions
PDF
Amcat automata questions
PDF
Assignment 6 as a reference public class ArrExample pr.pdf
PDF
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
PPTX
Advance excel
PDF
C programming assignment help
DOCX
Space Complexity in Data Structure.docx
PPTX
DS UNIT1_STACKS.pptx
PPTX
C Exam Help
PDF
Python_Module_2.pdf
PDF
computer notes - Stack
PDF
‏‏Lecture 2.pdf
PPT
computer notes - Data Structures - 2
PDF
Homework Assignment – Array Technical DocumentWrite a technical .pdf
PDF
java I am trying to run my code but it is not letting me .pdf
PDF
Acm aleppo cpc training sixth session
computer notes - List implementation
Linear Data Structures_SSD.pdf
data structures and algorithms Unit 3
Advanced s and s algorithm.ppt
Amcat automata questions
Amcat automata questions
Assignment 6 as a reference public class ArrExample pr.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Advance excel
C programming assignment help
Space Complexity in Data Structure.docx
DS UNIT1_STACKS.pptx
C Exam Help
Python_Module_2.pdf
computer notes - Stack
‏‏Lecture 2.pdf
computer notes - Data Structures - 2
Homework Assignment – Array Technical DocumentWrite a technical .pdf
java I am trying to run my code but it is not letting me .pdf
Acm aleppo cpc training sixth session
Ad

More from C++ Homework Help (20)

PPTX
cpp promo ppt.pptx
PPTX
CPP Homework Help
PPT
CPP homework help
PPTX
C++ Programming Homework Help
PPTX
Online CPP Homework Help
PPTX
CPP Homework Help
PPTX
C++ Homework Help
PPTX
C++ Programming Homework Help
PPTX
Get Fast C++ Homework Help
PPTX
Best C++ Programming Homework Help
PPTX
CPP Homework Help
PPTX
CPP Programming Homework Help
PPTX
CPP Homework Help
PPTX
Online CPP Homework Help
PPTX
CPP Assignment Help
PPTX
CPP Homework help
PPTX
CPP homework help
PPTX
CPP Homework Help
PPTX
CPP Homework Help
PPTX
Cpp Homework Help
cpp promo ppt.pptx
CPP Homework Help
CPP homework help
C++ Programming Homework Help
Online CPP Homework Help
CPP Homework Help
C++ Homework Help
C++ Programming Homework Help
Get Fast C++ Homework Help
Best C++ Programming Homework Help
CPP Homework Help
CPP Programming Homework Help
CPP Homework Help
Online CPP Homework Help
CPP Assignment Help
CPP Homework help
CPP homework help
CPP Homework Help
CPP Homework Help
Cpp Homework Help
Ad

Recently uploaded (20)

PPTX
Pharma ospi slides which help in ospi learning
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Pre independence Education in Inndia.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Institutional Correction lecture only . . .
PPTX
Lesson notes of climatology university.
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Cell Types and Its function , kingdom of life
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Classroom Observation Tools for Teachers
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Pharma ospi slides which help in ospi learning
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
TR - Agricultural Crops Production NC III.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
RMMM.pdf make it easy to upload and study
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Pre independence Education in Inndia.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Institutional Correction lecture only . . .
Lesson notes of climatology university.
Microbial disease of the cardiovascular and lymphatic systems
Cell Types and Its function , kingdom of life
O7-L3 Supply Chain Operations - ICLT Program
Classroom Observation Tools for Teachers
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
GDM (1) (1).pptx small presentation for students
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf

CPP Programming Homework Help

  • 1. For any Homework related queries, Call us at : - +1 678 648 4277 You can mail us at :- info@cpphomeworkhelp.com or reach us at :- https://www.cpphomeworkhelp.com/
  • 2. 1. Write a function named "location_of_largest" that takes as its arguments the following: (1) an array of integer values; (2) an integer that tells how many integer values are in the array. The function should return as its value the subscript of the cell containing the largest of the values in the array. Thus, for example, if the array that's passed to the function looks like this: then the function should return the integer 2 as its value. If there is more than one cell containing the largest of the values in the array, then the function should return the smallest of the subscripts of the cells containing the largest values. For example, if the array that's passed to the function is hen the largest value occurs in cells 2 and 5 , so the function should return the integer value 2 . 2. Write a function named "location_of_target" that takes as its arguments the following: (1) an array of integer values; (2) an integer that tells how many integer values are in the array; cpphomeworkhelp.com
  • 3. (3) an integer "target value". The function should determine whether the given target value occurs in any of the cells of the array, and if it does, the function should return the subscript of the cell containing the target value. If more than one of the cells contains the target value, then the function should return the largest subscript of the cells that contain the target value. If the target value does not occur in any of the cells, then the function should return the sentinel value −1 . Thus, for example, if the target value that's passed to the function is 34 and the array that's passed to the function looks like this: then the target value occurs in cells 3 and 5 , so the function should return the integer value 5 . 3. Write a function named "rotate_right" that takes as its arguments the following: (1) an array of floating point values; (2) an integer that tells the number of cells in the array; The function should shift the contents of each cell one place to the right, except for the contents of the last cell, which should be moved into the cell with subscript 0 . Thus, for example, if the array passed to the function looks like this: cpphomeworkhelp.com
  • 4. then when the function returns, the array will have been changed so that it looks like this: The function should not return a value. 4. Write a function named "shift_right" that takes as its arguments the following: (1) an array of floating point values; (2) an integer, call it "left", that tells the leftmost cell of the part of the array to be shifted; (3) an integer, call it "right", that tells the rightmost cell of the part of the array to be shifted; (4) a positive integer, call it "distance" that tells how many cells to shift by. The function should make sure that left is less than or equal to right, and that distance is greater than zero. If either of these conditions fails, the function should return the value 1 to indicate an error. Otherwise it should shift by distance cells the contents of the array cells with subscripts running from left to right . Thus, for example, if the array passed to the function looks like this: cpphomeworkhelp.com
  • 5. so that when the function returns, the array will have been changed so that it looks like this: The question marks in cells 3 and 4 indicate that we don't care what numbers are in those cells when the function returns. Note that the contents of cells 8 and 9 have changed, but the contents of cell 10 is unchanged. The function need not take any precautions against the possibility that the cells will be shifted beyond the end of the array (the calling function should be careful not to let that happen). 5. Write a function named "subtotal" takes as its arguments the following: (1) an array of floating point values; (2) an integer that tells the number of cells in the array. The function should replace the contents of each cell with the sum of the contents of all the cells in the original array from the left end to the cell in question. Thus, for example, if the array passed to the function looks like this: because 5.8 + 2.6 = 8.4 and 5.8 + 2.6 + 9.1 = 17.5 and so on. Note that the contents of cell 0 are not changed. The function should not return a value. cpphomeworkhelp.com
  • 6. 1. Here is the simplest solution. /************** L O C A T I O N O F L A R G E S T ************** DESCRIPTION: Finds the index of the largest number in an array. PARAMETERS: a An array of integers to be scanned. n The number of objects in the array, starting at cell 0. RETURNS: The index of the cell containing the largest integer. If the largest integer appears in more than one cell, then the index of the leftmost cell where it appears will be returned. ALGORITHM: A variable that will hold the index of the largest integer is initialized to zero, and we pretend to have scanned . that cell and found that it contains the largest integer seen "so far". Then successive cells are examined and compared with the cell containing the largest integer so far. When a cell containing a new largest integer is encountered, the variable that holds the index of the largest integer seen so far is modified to the new index AUTHOR: W. Knigh ******************************************************************* / cpphomeworkhelp.com
  • 7. int location_of_largest (const int a[], int n) { int best = 0; // Location of the largest so far. int i; for (i = 1; i < n; ++i) // Start comparing at the second cell. if (a[i] > a[best]) best = i; return best; } The following solution is not as elegant as the one on the preceding page. /************** L O C A T I O N O F L A R G E S T ************** DESCRIPTION: Finds the index of the largest number in an array. PARAMETERS: a An array of integers to be scanned. n The number of objects in the array, starting at cell 0. RETURNS: The index of the cell containing the largest integer. If the largest integer appears in more than one cell, then the index of the leftmost cell where it appears will be returned. cpphomeworkhelp.com
  • 8. ALGORITHM: A variable that will hold the index of the largest integer is initialized to zero, and we pretend to have scanned that cell and found that it contains the largest integer seen "so far". We initialize a "largest_value" variable to the value a[0]. Then successive cells are examined and compared with the value of the largest integer so far. When a cell containing a new largest integer is encountered, the variable that holds the index of the largest integer seen so far is modified to the new index, and the largest_value variable is updated. AUTHOR: W. Knight ******************************************************************* / int location_of_largest (const int a[], int n) { int largest_value = a[0]; // First cell contains largest so far. int best = 0; // Location of the largest so far. int i; for (i = 1; i < n; ++i) if (a[i] > largest_value) { largest_value = a[i]; best = i; cpphomeworkhelp.com
  • 9. } return best; } The following is NOT an acceptable solution because it does not allow for the possibility that all the integers in the array may be negative or zero. int location_of_largest (const int a[], int n) // NOT ACCEPTABLE { int largest_value = 0; // Assume the largest value will be > 0. int best; // Eventual location of the largest value. int i; for (i = 0; i < n; ++i) if (a[i] > largest_value) // Will be true for some i only if { // the array contains at least one largest_value = a[i]; // value strictly greater than 0. best = i; } return best; } The incorrect solution above can be corrected by initializing largest_value to the most negative possible integer value (INT_MIN, which is defined in the header file <limits.h>) and initializing best to 0 . cpphomeworkhelp.com
  • 10. 2. Here is the simplest solution. It searches from right to left and quits when (if) if finds the target. /************** L O C A T I O N O F T A R G E T ************** DESCRIPTION: Finds the index of the cell (if any) where a "target“ integer is stored. PARAMETERS: a An array of integers to be scanned. n The number of objects in the array, starting at cell 0. target The integer that we hope to find in some cell of array a. RETURNS: The index of the cell containing the integer "target" provided there is such a cell. If there is not, then the function returns -1 as a sentinel value. If the target integer appears in more than one cell, then the index of the rightmost cell where it appears will be returned. ALGORITHM: The value parameter "n" is used to scan backward across the array, looking for a cell containing a copy of "target". If a copy is found, the search is halted and the index of that cell is returned. If no such cell is found, "n" will run off the left end of the array and wind up with value -1. AUTHOR: W. Knight *************************************************************** cpphomeworkhelp.com
  • 11. / int location_of_target (const int a[], int n, int target) { --n; // Make n "point" to the last cell of the array. while (n >= 0 && a[n] != target) // Search right to left --n; return n; // Returns -1 if target is not in array a[]. } The following solution is acceptable, although it is not in general as efficient as the one above because it always examines every cell of the array. /************** L O C A T I O N O F T A R G E T ************** DESCRIPTION: Finds the index of the cell (if any) where a "target“ integer is stored. PARAMETERS: a An array of integers to be scanned. n The number of objects in the array, starting at cell 0. target The integer that we hope to find in some cell of array a. RETURNS: The index of the cell containing the integer target, provided there is such a cell. If there is not, then the function returns -1 as a sentinel value. If the target integer appears in more than one cell, then the index of the rightmost cell where it appears will be returned. cpphomeworkhelp.com
  • 12. ALGORITHM: A location variable is initialized to -1 in case no copy of "target" is found. Then each cell of the array is examined, starting at the left end, and each time a copy "target" is found, the location variable is updated to the index of that cell. AUTHOR: W. Knight ******************************************************************* / int location_of_target (const int a[], int n, int target) { int location = -1; // Target not seen yet. int i; for (i = 0; i < n; ++i) if (a[i] == target) location = i; return location; } cpphomeworkhelp.com
  • 13. 3. /********************* R O T A T E R I G H T ********************* DESCRIPTION: Shifts the contents of array cells one cell to the right, with the last cell's contents moved to the left end. PARAMETERS: a The floating point array to be modified. n The number of objects in the array, starting at cell 0. RETURNS: Void (no value). ALGORITHM: The object in the right-most cell is copied to a temporary location, and then the object each cell to the left of the last cell is copied to its immediate right neighbor; the process moves from right to left. Finally, the object in the temporary location is copied to the leftmost cell. AUTHOR: W. Knight ******************************************************************* / void rotate_right (float a[], int n) { float temp = a[n-1]; // Hold the contents of the last cell. int i; for (i = n - 1; i >= 1; --i) a[i] = a[i-1]; a[0] = temp; } cpphomeworkhelp.com
  • 14. 4. /*********************** S H I F T R I G H T ********************* DESCRIPTION: Shifts the contents of some subarray in an array to the right by a specified number of cells. PARAMETERS: a The floating point array to be modified. left The index of the leftmost cell of the subarray to be shifted. right The index of the rightmost cell of the subarray. distance The number of cells by which the subarray will be shifted. RETURNS: 1 provided left <= right and distance > 0; returns 0 if either of these conditions is violated. ALGORITHM: An index variable is initialized to "point" to the rightmost cell of the subarray to be shifted; another index variable is initialized to point to the cell to which the rightmost object will be copied. Then the copying takes place and the indexes are moved to the left (decremented by 1). The occurs repeatedly until the object in the leftmost cell of the subarray has been copied. AUTHOR: W. Knight ******************************************************************* / cpphomeworkhelp.com
  • 15. int shift_right (float a[], int left, int right, int distance) { if (left > right || distance <= 0) return 1; int i = right, // points to the cell to be shifted j = i + distance; // points to the receiving cell while (i >= left) { a[j] = a[i]; --i; --j; } return 0; } Here is a slightly more elegant version. /*********************** S H I F T R I G H T ********************* DESCRIPTION: Shifts the contents of some subarray in an array to the right by a specified number of cells. PARAMETERS: a The floating point array to be modified. left The index of the leftmost cell of the subarray to be shifted. cpphomeworkhelp.com
  • 16. right The index of the rightmost cell of the subarray. distance The number of cells by which the subarray will be shifted. RETURNS: 1 provided left <= right and distance > 0; returns 0 if either of these conditions is violated. ALGORITHM: An index variable is initialized to "point" to the rightmost cell of the subarray to be shifted. Then the object in that cell is copied to the cell that's "distance" units to the right. Then the index is decremented by 1 and the same process is repeated. This continues until all objects in the subarray have been copied. AUTHOR: W. Knight ******************************************************************* / int shift_right (float a[], int left, int right, int distance) { if (left > right || distance <= 0) return 1; int i; for (i = right; i >= left; --i) a[i + distance] = a[i]; return 0;} cpphomeworkhelp.com
  • 17. 5. /************************* S U B T O T A L *********************** DESCRIPTION: Replaces each number in an array with the sum of all the numbers up to that location in the original array. PARAMETERS: a The floating point array to be modified. n The number of objects in the array, starting at cell 0. RETURNS: Void (no value). ALGORITHM: Starting with cell 1 and moving right, the number in each cell is replaced by the sum of that number and the sum that's now in the cell just to the left. AUTHOR: W. Knight ******************************************************************* / void subtotal (float a[], int n) { int i; for (i = 1; i < n; ++i) a[i] += a[i-1]; cpphomeworkhelp.com