In: Computer Science
C PROGRAMMING LANGUAGE
PROBLEM TITLE : ARRAY
usually, if people want to input number into an array, they will put it from index 0 until N - 1 using for. But, Bibi is bored to code like that. So, she didin't want to input the number that way.
So Bibi challenged you to make a program that will read a sequence (represent index) that she made, then input the number to an array but input it with the same sequence as sequence that Bibi gave.
Format Input
The first line represent integer N the size of Bibi's Array. The next line consist N integers Ai represent the sequence that Bibi want, it is guaranteed that the number is distinct. The next line consist N integers represent the value that she want to put inside array with index Ai.
Format Output
N integers represent the array that Bibi has starting from index 0.
Constraints
• 1 ≤ N ≤ 1, 000
• 0 ≤ Ai < N
Sample Input 1 (standard input)
5
0 1 2 3 4
1 2 3 4 5
Sample Output 1 (standard output)
1 2 3 4 5
Sample Input 2 (standard input)
5
4 3 2 1 0
1 2 3 4 5
Sample Output 2 (standard output)
5 4 3 2 1
sample Input 3 (standard input)
5
0 4 3 1 2
1 2 3 4 5
Sample Output 3 (standard output)
1 4 5 3 2
NOTES
• There isn’t any space after the last number.
• In the third sample Bibi want to input the number to array index 0 then 4 then 3 then 1 then 2
(MAKE THE COMPILER UNTIL SAMPLE 3)
C programming for above problem
#include <stdio.h>
int main()
{
int x;
int num[5];
int pos[5];
int n,number,i;
// loop runs upto 3 times
for (i=0; i<3;i++){
// prompting and taking array
printf("\nSample Input %d (standard
input)",i+1);
printf("\nEnter number of elements: ");
scanf("%d", &n);
// declaring array with all zeros
int temp[5]={0,0,0,0,0};
/* We are using a for loop to traverse through the
array
* while storing the entered values in the array
*/
printf("\nEnter positions\n ");
for (x=0; x<n;x++)
{
// taking inputs from the
user
scanf("%d", &pos[x]);
}
printf("\nEnter numbers\n");
for (x=0; x<n;x++)
{
scanf("%d", &number);
// condition check 1 <= N <= 1000
if(number>=1 && number <=1000){
temp[pos[x]]=number;
}
}
printf(" \nSample Output %d (standard
output)\n",i+1);
for (x=0; x<n;x++)
{
// printing the array
printf("%d ", temp[x]);
}
}
return 0;
}
OUTPUT SCREEN
#include <stdio.h>
int main()
{
int x;
// declaring arrays
int num[5];
int pos[5];
int n,number,i;
// loop runs upto 3 times
for (i=0; i<3;i++){
// prompting and taking array
printf("\nSample Input %d (standard input)",i+1);
printf("\nEnter number of elements: ");
scanf("%d", &n);
// declaring array with all zeros
int temp[5]={0,0,0,0,0};
/* We are using a for loop to traverse through the array
* while storing the entered values in the array
*/
printf("\nEnter positions\n ");
for (x=0; x<n;x++)
{
// taking inputs from the user
scanf("%d", &pos[x]);
}
printf("\nEnter numbers\n");
for (x=0; x<n;x++)
{
scanf("%d", &number);
// condition check 1 <= N <= 1000
if(number>=1 && number <=1000){
temp[pos[x]]=number;
}
}
printf(" \nSample Output %d (standard output)\n",i+1);
for (x=0; x<n;x++)
{
// printing the array
printf("%d ", temp[x]);
}
}
return 0;
}