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
  • 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...

  • 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...

  • In C language Write a program that includes a function search() that finds the index of...

    In C language Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function, and displays...

  • Write a function named numPerfect that takes as parameters: an array of integer scores between zero...

    Write a function named numPerfect that takes as parameters: an array of integer scores between zero and 100 (inclusive) the size of the array and returns the number of perfect scores in the array. Also write a main function that creates and initializes an array of ints (in the appropriate range), calls the function with that array, and prints out the return value.

  • Write a JAVA PROGRAM with function named getData() that takes a matrix A and an integer...

    Write a JAVA PROGRAM with function named getData() that takes a matrix A and an integer "row" as input parameters and returns an array containing all elements in the given row of A.

  • Program must be wriiten in c++ Write a function, removeAt, that takes three parameters: an array...

    Program must be wriiten in c++ Write a function, removeAt, that takes three parameters: an array of integers, the number of elements in the array, and an integer (say, index). The function should delete the array element indicated by index. If index is out of range or the array is empty, output an appropriate message. (Note that after deleting the element, the number of elements in the array is reduced by 1.) Assume that the array is unsorted.

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
ADVERTISEMENT