Question

[ Write in C not C++] Define a C function void placeElements(int *a, int i, int...

[ Write in C not C++]

Define a C function void placeElements(int *a, int i, int j) that
inter-exchange the array elements at indexiandj. For a[] = {5,7,4,2,1},a
callto placeElements(a,2,4) makesa[] = {5,7,1,2,4}. [8]

Homework Answers

Answer #1
#include <stdio.h>

void placeElements(int *a, int i, int j);

int main() {
    int a[] = {5, 7, 4, 2, 1}, i;
    for (i = 0; i < 5; ++i) {
        printf("%d ", a[i]);
    }
    printf("\n");
    placeElements(a, 2, 4);
    for (i = 0; i < 5; ++i) {
        printf("%d ", a[i]);
    }
    printf("\n");
    return 0;
}

void placeElements(int *a, int i, int j) {
    int temp = a[i];
    a[i] = a[j];
    a[j] = temp;
}

Know the answer?
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
Similar Questions
/************************************************************************************* Function Prototypes *************************************************************************************/ int ScanArray(char *fname, int myArray[], int nSize); void Ge
/************************************************************************************* Function Prototypes *************************************************************************************/ int ScanArray(char *fname, int myArray[], int nSize); void GenerateFromArray(void); /************************************************************************************/ /************************************************************************************/ int main(void) { /* This is the main() program. It should call the functions ScanArray() and GenerateFromArray() */ GenerateFromArray(); system("pause"); return 0; } /*************************************************************************************** Define this function to scan the elements of an array from the given data file "ArrayInp.dat". If the input file is not found, or contains invalid data, the function should return a 0 and print an error message. If the input...
Write a function with the prototype void stars(int i);. The function prints a line of i...
Write a function with the prototype void stars(int i);. The function prints a line of i stars followed by a new line. Write code to apply the stars function to every node in some binary tree called Tree using an in-order traversal, where the number of stars printed is given by the data of the node in the BST. You must write both the function definition and the code that uses it with the star function.
C Programming 1. Write a void function that takes in a 2D array of character and...
C Programming 1. Write a void function that takes in a 2D array of character and have it print out each array on a new numbered line on the console. 2. Illustrate the stack and the heap allocation. Specify what each variable value holds and where different references are pointing to. int main() { int myarray[3]; myfunction (myarray); } int myfunction(int* in) { int i; for (i = 0; i<3; i+= 1) { in[i] = i; } // illustrate the...
Consider the following function: double arrayavg(int a[], int n){ int sum=0; for(int i=0; i!=n; ++i){ sum+=a[i];...
Consider the following function: double arrayavg(int a[], int n){ int sum=0; for(int i=0; i!=n; ++i){ sum+=a[i]; } return (double)sum/n; } Rewrite the function to eliminate the array subscripting (a[i]), using pointer arithmatic instead. Write a program that reads a line of input and checks if it is a palindrome (ignoring spaces) Write a program that takes the name of a file as a command line argument, and prints the contents of the file (similar to the linux 'cat' program). Write...
Write a c++ function which takes two parameters: an array of ints and an int size...
Write a c++ function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void printSome(const int array[], int size);
Write a MIPS assembly program that sorts an array using bubble sort translating the C code...
Write a MIPS assembly program that sorts an array using bubble sort translating the C code int main(void) { int array[] = {10, 2, 7, 5, 15, 30, 8, 6}; // input array int arraySize = sizeof(array)/sizeof(array[0]); bool swapped = true; int j = 0; int tmp; while (swapped) { swapped = false; //Note : "j" , "arraySize - j" are optimizations to the bubble sort algorithm j++; // j= sorted elements int i=0; /* "arraySize - j" is used...
C++ questions 5.Which of the following function prototype(s) are correct. void setValue(int &value); void setValue(int &);...
C++ questions 5.Which of the following function prototype(s) are correct. void setValue(int &value); void setValue(int &); void setValue(int value, int max=100); void setValue(int ++value); 6. T/F Calling an overloaded function can result in multiple values being returned to the caller. 7.A ____ function has ____ return value(s), but can have ____ parameter(s). Otherwise a function can return _____ value(s). 8.T/F Only variables can be passed as reference. 9.T/F When writing a C++ program you must be careful to arrange all...
_delay_ms( ) function accepts only a constant in milliseconds as parameter, write a function void delay(int...
_delay_ms( ) function accepts only a constant in milliseconds as parameter, write a function void delay(int a) that produces a delay where “a” is a variable in C
How would I write this function in C programming language? Function header: int input(int *a, int...
How would I write this function in C programming language? Function header: int input(int *a, int *b, int *c) This function should attempt to input 3 integers from the keyboard and store them in the memory locations pointed to by a, b, and c. The input may not be successful in reading all three values. The function should return the number of values that were successfully read.
Consider the following function: 01: void cpUniq (const int* source, int N, vector<int>& dest) 02: {...
Consider the following function: 01: void cpUniq (const int* source, int N, vector<int>& dest) 02: { 03: list<int> L; 04: for (int i = 0; i < N; ++i) 05: { 06: copy (source, source + i, front_inserter<int>(L)); 07: } 08: for (int j: L) 09: { 10: if (find(dest.begin(), dest.end(), j) != dest.end()) 11: dest.push_back(j); 12: } 13: } and the following list of complexity values: A: O(1), B: O(log N), C: O(N), D: O(N log N), E: O(N2),...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT