Question

Given an array and a starting position write a function replaceFromN, that takes an integer array...

Given an array and a starting position write a function replaceFromN, that takes an integer array 'array', the size of the array size and a starting positions 'n' as parameters and replaces the elements starting from that index onward with the sequence 1,2,3,... The function returns nothing.

void replaceFromN(int array[], int size, int n)

For example, given

array= {15,12,4,9,2,3}

n =2

the function should modify array to be {15,12,1,2,3,4}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

using namespace std;

// Function declaration
void replaceFromN(int array[], int size, int n);
int main()
{
// Declaring an array
int array[] = { 15, 12, 4, 9, 2, 3 };

// Displaying the elements of an array
cout << "Displaying the elements in the array before replacing :" << endl;
for (int i = 0; i < 6; i++)
{
cout << array[i] << " ";
}

/* calling the function by passing
* array,size,index val as arguments
*/
replaceFromN(array, 6, 2);
return 0;
}

/* This function will replace the
* elements of an aray from the index n
* with sequence of values
*/
void replaceFromN(int array[], int size, int n)
{
int seq = 0;
for (int i = 0; i < size; i++)
{
if (i >= n)
{
array[i] = ++seq;
}
}

// Displaying the elements of array
cout << "\nDisplaying the elements in the array after replacing :" << endl;
for (int i = 0; i < size; i++)
{
cout << array[i] << " ";
}
}

__________________

Output:

CAProgram Files (x86)\Dev-CpplMinGW64\bin ReplacingElementsOfArrayWithSequence.exe Displaying the elements in the array befor

_________Thank YOu

Add a comment
Know the answer?
Add Answer to:
Given an array and a starting position write a function replaceFromN, that takes an integer array...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • *****************************CODE SHOULD BE WRITTEN IN C++************************ Write a function to reverse an array of integers starting...

    *****************************CODE SHOULD BE WRITTEN IN C++************************ Write a function to reverse an array of integers starting from a given start index and a given end index. Note, the end index is inclusive. Add code to the given function below. *********************FUNCTION TO BE USED*********************************************** #include "problem1.h" void reverseArray(int arr[], int start, int end) { //written code } ************************************EXAMPLE************************************************ int[] a = {1,2,3}; reverseArray(a, 0, 2); //The content of a should be {3,2,1}. int[] a = {1,2,3}; reverseArray(a, 0, 1); //The content...

  • Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...

    Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. To test your function, write a main that prompts a user for a list of 15 integers and outputs the index and value of the first occurrence of the smallest value. The program should print out Enter 15 integers: The position of the first occurrence of the smallest element in...

  • Given an integer array a[ ] of N elements. Please write an OpenMP function to sort...

    Given an integer array a[ ] of N elements. Please write an OpenMP function to sort it by the Quicksort algorithm using the task directive. The function header is: void quicksort(int *a, int p, int r). (p represents the start index and r represents the end index)

  • (C++) Write a function, insertAt, that takes four parameters: an array of integers, the number of...

    (C++) Write a function, insertAt, that takes four parameters: an array of integers, the number of elements in the array, an integer (say, insertItem), and an integer (say, index). The function should insert insertItem in the array provided at the position specified by index. If index is out of range, output the following: Position of the item to be inserted is out of range or if the index is negative: Position of the item to be inserted must be nonnegative...

  • In C: Write a function "MinMax" that takes as an argument an integer array, an integer...

    In C: Write a function "MinMax" that takes as an argument an integer array, an integer for the size of the array, and two integer pointers. The function should print nothing and return nothing but change the value of the first pointer to the minimum value in the array and change the value in the second pointer to the max value in the array.

  • in C++ and also each function has its own main function so please do the main...

    in C++ and also each function has its own main function so please do the main function as well. Previously when i asked the question the person only did the function and didn't do the main function so please help me do it. 1-1. Write a function that returns the sum of all elements in an int array. The parameters of the function are the array and the number of elements in the array. The function should return 0 if...

  • public static int[] collatz(int start, int numIterations) Given integer start and integer numIterations, return an array...

    public static int[] collatz(int start, int numIterations) Given integer start and integer numIterations, return an array containing the Collatz sequence beginning with start up to numIterations. The Collatz function is defined by: 3n + 1 if n is odd n/2 if n is even Given start = 7 and numIterations = 3, this method returns [7, 22, 11, 34] Parameters: start - starting integer numIterations - how long to compute the Collatz sequence for Returns: an array containing the Collatz...

  • Write a function getScores(...) that is given a string, an int type array to fill and...

    Write a function getScores(...) that is given a string, an int type array to fill and the max number of elements in the array as parameters and returns an integer result. The function will find each substring of the given string separated by space characters and place it in the array. The function returns the number of integers extracted. For example: int values[3]; getScores("123 456 789", values, 3 ); would return the count of 3 and fill the array with...

  • (JAVA) Given an array of unique positive integers, write a function findSums that takes the array...

    (JAVA) Given an array of unique positive integers, write a function findSums that takes the array input and: 1. Creates a hashtable called “hashT” and inserts all the elements of the input array in the hashtable. 2. Finds pairs of elements in the hashtable whose sum is another element (sum) in the hashtable and print the pairs in the console. 3. Returns another hashtable names “sums” of those sum elements. For example: Input: [1,5,4,6,7,9] Output: [6,5,7,9] Explanation: 6 = 1...

  • Write a C++ function binsearch that carries out the binary search algorithm on a sorted array...

    Write a C++ function binsearch that carries out the binary search algorithm on a sorted array of integers. Your function takes as parameters an array of integers (sorted in increasing order), the size of the array, and a target integer to search for. The function returns the index of the target in the array, and returns -1 if the target is not in the array. The file main1.txt on the webpage contains code that generates a specific array of integers...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT