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 this code in c++. Using iostream and cstring library. Write a function that will...
Please, write this code in c++. Using iostream and cstring library. Write a function that will delete all words in the given text that meets more that one time. Also note than WORD is sequence of letters sepereated by whitespace. Note. The program have to use pointer. Input: First line contains one line that is not longer than 1000 symbols with whitespaces. Each word is not longer than 30 symbols. Output: Formatted text. example: input: Can you can the can...
Please, write code in c++. Using iostream and cstring library Write a function that will find...
Please, write code in c++. Using iostream and cstring library Write a function that will find and return most recent word in the given text. The prototype of the function have to be the following void mostRecent(char *text,char *word) In char *word your function have to return the most recent word that occurce in the text. Your program have to be not case-sensitive(ignore case - "Can" and "CAN" are the same words) Also note than WORD is sequence of letters...
Write the pseudocodes for these programs: a) #include <iostream> using namespace std; void print(int arr[],int n){...
Write the pseudocodes for these programs: a) #include <iostream> using namespace std; void print(int arr[],int n){ if(n==0) return; cout<<arr[n-1]<<" "; print(arr,n-1); } int main() { int n; cout<<"Enter the size of the array:"; cin>>n; int arr[n]; int i; cout<<"Enter the elements of the array:"<<endl; for(i=0;i<n;i++){ cin >> arr[i]; } cout<<"Displaying the elements of the array in reverse order:"<<endl; print(arr,n); return 0; } b) #include<iostream> #include<fstream> using namespace std; //student structure struct student { int id; float gpa; }; //node structure...
Please, write code in c++. Using iostream library A chessboard pattern is a pattern that satisfies...
Please, write code in c++. Using iostream library A chessboard pattern is a pattern that satisfies the following conditions: • The pattern has a rectangular shape. • The pattern contains only the characters '.' (a dot) and 'X' (an uppercase letter X). • No two symbols that are horizontally or vertically adjacent are the same. • The symbol in the lower left corner of the pattern is '.' (a dot). You are given two numbers. N is a number of...
Please write code in c++ using iostream library. Also you can use any string library. Create...
Please write code in c++ using iostream library. Also you can use any string library. Create structure plane with the following: 1)plane's ID[int type] 2) location[char*] 3) departure time[char*] Your task is to find the plane with the smallest departure time for the selected city. Pointer, dynamic array and structures have to be used in this code. Input: NOTE: It's just an example of input, you should write code generally for any inputs. First line contains n(0 < n <...
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...
Please, write code in c++. Using iostream and cstring library. You given a text.Your task is...
Please, write code in c++. Using iostream and cstring library. You given a text.Your task is to write a function that will find the longest sequence of digits inside. Note that the output have to be presened just like in sample. Note. The program have to use pointer. Input: First line contains one line that is not longer than 1000. Output: The longest sequence of numbers.All numbers are positive and integers. example: input: 101 fdvnjfkv njfkvn fjkvn jffdvfdvfd2010 output: 2010
Please write code in c++ using iostream library. Write a function bool cmpr(char * s1, int...
Please write code in c++ using iostream library. Write a function bool cmpr(char * s1, int SIZE1, char * s2, int SIZE2) that compares two strings. Input Input contains two strings. Each string is on a separate line. example: aqua aqua Output Output YES if given two strings are the same or NO otherwise. YES
Please, write code in c++. Using iostream library Most modern text editors are able to give...
Please, write code in c++. Using iostream library Most modern text editors are able to give some statistics about the text they are editing. One nice statistic is the average word length in the text. A word is a maximal continuous sequence of letters ('a'-'z', 'A'-'Z'). Words can be separated by spaces, digits, and punctuation marks. The average word length is the sum of all the words' lengths divided by the total number of words. For example, in the text...
Please, write code in c++. Using iostream and cstring library Use pointers! You given a text.Your...
Please, write code in c++. Using iostream and cstring library Use pointers! You given a text.Your task is to write a function that will find the longest sequence of digits inside. Note that the output have to be presened just like in sample. Note. The program have to use pointer. Input: First line contains one line that is not longer than 1000. Output: The longest sequence of numbers.All numbers are positive and integers. Samples: № INPUT OUTPUT 1 This is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT