Question

In: Computer Science

write this program in c++ using iostream library.( cin>>n; cin>> arr[n] and so on) Write a...

write this program in c++ using iostream library.( cin>>n; cin>> arr[n] and so on)

Write a function that converts an array so that in the first half settled with elements from odd positions, and in the second half - with elements from the even positions.Positions are counted from the first index.The program have to use pointer.

example: input:

7

1 2 3 4 5 6 7

output: 1 3 5 7 2 4 6 8

Solutions

Expert Solution

Program

#include <iostream>
using namespace std;

/* converts an array so that in the first half settled with elements from odd positions, and in the second half - with elements from the even positions */

void convert(int *a, int n)
{
int k = 0, j, t;
for(int i=0; i<n/2-1; i++)
{
k += 2;
t = *(a+k);
for(j=k-1; j>=i+1; j--)
{
*(a+j+1) = *(a+j);
}
*(a+j+1) = t;
}
}

//main function
int main()
{
int n;
cout<<"Enter n:";
cin >> n;

int *a = new int[n];
cout<<"Enter the array elements: "<<endl;
for(int i=0;i<n; i++)
{
cin >> *(a+i);
}

convert(a, n);

cout<<"Converted array:"<<endl;
for(int i=0;i<n; i++)
{
cout << *(a+i) << " ";
}

return 0;
}

Output:

Enter n:8
Enter the array elements:
1 2 3 4 5 6 7 8
Converted array:
1 3 5 7 2 4 6 8

Enter n:7
Enter the array elements:
1 2 3 4 5 6 7
Converted array:
1 3 5 2 4 6 7

Note: Your feedback is very important for us, I expect positive feedback from your end.


Related Solutions

Please, write code in c++. Using iostream and cstring library. Your friend is the person who...
Please, write code in c++. Using iostream and cstring library. Your friend is the person who does not like any limitations in the life. And when you said to him that it is totally impossible to work with integer numbers bigger than 4 294 967 296 in C++ he blamed you in time-wasting during the university study.So to prove that you hadn't waste 2 months of your life studying C++ in university you have to solve this issue. Your task...
Write a C++ program that prints a calendar for a given year. ONLY USING "#include<iostream>" and...
Write a C++ program that prints a calendar for a given year. ONLY USING "#include<iostream>" and "#include<cmath>" The program prompts the user for two inputs:       1) The year for which you are generating the calendar.       2) The day of the week that January first is on, you will use the following notation to set the day of the week:       0 Sunday                     1 Monday                   2 Tuesday                   3 Wednesday       4 Thursday                 5 Friday                      6 Saturday Your program should...
Write a C++ program (using the pthread library) that accepts a phrase of unspecified length on...
Write a C++ program (using the pthread library) that accepts a phrase of unspecified length on the command line. For example: prompt$: ./vowcon Operating Systems Class at CSUN The main() in this program should read the phrase from the terminal. This phrase can be read into a global variable. This phrase or its parts can be directly accessed from the main() and from the threads. The main() has to create two threads running functions (vow and con). The main() can...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must be very basic. Please don't use math sqrt. Assume that the user does not input anything less than 0. For example: the integer square root of 16 is 4 because 4 squared is 16. The integer square root of 18 is 5 because 4 squared is 16 and 5 squared is 25, so 18 is bigger than 16 but less than 25.  
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std;...
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std; void leapYear(int x); int main() { int x; cout << "Enter a year: "; cin >> x; leapYear (x);   return 0; } void leapYear(int x ) {    if (x % 400 == 0)    {    cout << "This is a leap Year";}    else if    ((x % 4 == 0) && (x % 100 != 0))    {    cout <<...
Write a statement to call prepare Arr to set values for the array (Using C++) #include...
Write a statement to call prepare Arr to set values for the array (Using C++) #include using namespace std; const int NUM = 10; void prepareArr(int a[]); int countEven (int b[]); int main() { int arr[NUM]; // write a statement to call prepareArr to set values for the array // write a statement to call countEven and print the data returned for(int i = 0; i cout << arr[i] <<" "; cout < return 0; } void prepareArr(int a[]) {...
A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using...
A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using namespace std; int lastIndexOf(char *s, char target) { int n=strlen(s); for(int i=n-1;i>=0;i--) { if(s[i]==target) { return i; } } return -1; } void reverse(char *s) { int n=strlen(s); int i=0,j=n-1; while(i<=j) { char temp=s[i]; s[i]=s[j]; s[j]=temp; i++; j--; } return; } int replace(char *s, char target, char replacementChar) { int len=strlen(s); int total=0; for(int i=0;i<len;i++) { if(s[i]==target) { s[i]=replacementChar; total+=1; } } return total;...
Write a program in JAVA using the BigInteger library that can be used to check a...
Write a program in JAVA using the BigInteger library that can be used to check a RSA signature, based on the signer's RSA public key pair. To test your program, take the following information about a message Alice signed and use the verify signature to reproduce the message Alice signed and convert it back to String format. n = 68236588817658935156357212288430888402056854883696767622850112840388111129987 e = 65537 signature = 46612763171375975923246342580942010388414761162366281695045830390867474569531
Write a small program to encrypt and decrypt a message using Python library.
Write a small program to encrypt and decrypt a message using Python library.
C++ Develop program in C++ using arrays of characters, subscript operator, the cstring library, and functions...
C++ Develop program in C++ using arrays of characters, subscript operator, the cstring library, and functions with arguments. Create programs with small functions where main goes to a series of functions where the real work takes place. Don’t use global variables and don’t use break within a loop (unless working with a switch statement). Functions can’t have more than 30 statements of code, not including comments, blank lines, or variable definitions. Don’t use a return in the middle of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT