Question

In: Computer Science

Use C language , pointer limit use //#include <stdio.h> //#include <stdlib.h> //#include <time.h> For example, I...

Use C language , pointer

limit use

//#include <stdio.h>

//#include <stdlib.h>

//#include <time.h>

For example,

I have random array [4,2,7,1,9,8,0].

Sort the array's index but cannot change the position of item in the array, if you copy the array to a new array, you also cannot change the item's position in array.

The index now is[0,1,2,3,4,5,6]

The output should be[6,3,1,0,2,5,4]

Solutions

Expert Solution

#include <stdio.h>

void sort(int n, int* ptr)
{
    int i, j, k;

    // sorting the numbers using pointer references
    for (i = 0; i < n; i++) {

        for (j = i + 1; j < n; j++) {

            if (*(ptr + j) < *(ptr + i)) {

                k = *(ptr + i);
                *(ptr + i) = *(ptr + j);
                *(ptr + j) = k;
            }
        }
    }


int main()
{
    int n, i,j,arr[], copy[];

    printf("Enter the required size of the array");//taking the size as input
    scanf("%d", &n);

    printf("Enter the elements of the array");
    for(i=0; i<n; i++)
    {
      printf("Enter the %dth element of the array: ", i);
      scanf("%d", &arr[i]);
    }

    arr= new int[n];
    copy= new int [n];//copy[] is of the same size as arr[] and holds the elements of arr[] in the same order

    for(i=0; i<n; i++)
    {
   copy[i]= arr[i];
     }

    sort(n, arr); //calling sort() to sort using pointers

    for(i=0; i<n; i++)
    {
   for(j=0; j<n; j++)
   {
       if(arr[i]==copy[j])//matching the elements of the sorted array with the unsorted one
           printf("%d", j);//the original indices of the sorted elements
   }
    }
              
    return 0;
}


Related Solutions

C program. librarys that are in use; stdio.h, stdlib.h, time.h, stddef.h, ctype.h, math.h, string.h. Need to...
C program. librarys that are in use; stdio.h, stdlib.h, time.h, stddef.h, ctype.h, math.h, string.h. Need to rewrite the functions linked in the code: "return add_move_lib ( board, col, colour ) ;" "return board_full_lib ( board ) ;" "return display_board_lib ( board ) ; // TASKS // board_full() display_board() add_move() // adds a token of the given value (1 or 2) to the board at the // given column (col between 1 and COLS inclusive) // Returns 0 if successful, -1...
#include<stdio.h> #include<stdlib.h> int main() {     //Q1) Note: You can use only pointer based operations while...
#include<stdio.h> #include<stdlib.h> int main() {     //Q1) Note: You can use only pointer based operations while implementing each of the functionalities below    // zero points will be given if pointer operations are not used in implementing    // each of the operations below.    // Also, use C code only in visual studio or GCC in general.asu.edu server    /* i.) Create a 2 - D array of integers using pointers (use dynamic memory allocation).            Assume that...
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid;...
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid; // Main process's PID=42 pid = fork(); // creates process with PID=36 if (pid == 0) { pid_t pid2 = fork(); // creates process with PID=99 sleep(10); if (pid2 > 0) { sleep(10); exit(0); } else { sleep(30); printf("** ONE **\n"); exit(0); } } else { pid_t pid3 = fork(); // creates process with PID=71 if (pid3 == 0) { sleep(30); exit(0); } pid_t...
LANGUAGE: C Only using <stdio.h> & <stdlib.h> Write a program that gives the user a menu...
LANGUAGE: C Only using <stdio.h> & <stdlib.h> Write a program that gives the user a menu to choose from – 1. Convert temperature input from the user in degrees Fahrenheit to degrees Celsius 2. Convert temperature input from the user in degrees Celsius to degrees Fahrenheit 3. Quit. Formulae you will need: C = (5 / 9) * (F-32) and F = (9/5) * C + 32 1. Use functions to accomplish 1 and 2 above. 2. Use at least...
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double...
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double fact (double); void main () { int angle_in_D; double term, angle_in_R; float sine = 0; unsigned int i = 1; double sign = 1; int n = 1000; printf ("Please enter an angle (Unit: Degree): "); scanf ("%d", &angle_in_D); angle_in_R = angle_in_D * M_PI / 180.0; do { term = pow(-1,(i-1)) * pow (angle_in_R, (2*i - 1)) / fact (2*i - 1); sine =...
how would i change the for loops to while loops in the code below #include<stdio.h> #include<stdlib.h>...
how would i change the for loops to while loops in the code below #include<stdio.h> #include<stdlib.h> int main() { int seed; // Taking seed value as input from the user printf("Enter a seed value (0 to quit): \n"); scanf("%d", &seed); // Running the loop until user enters 0 to quit // count array will count frequency of 0 , 1 , 2 ,3 int count[4]; for (int i = 0; i < 4; i++) count[i] = 0; while (seed !=...
Use the given Strings.c and Strings.h module: Strings.c: #include "Strings.h" #include <string.h> #include<stdlib.h> #include<stdio.h> char* substring(char*...
Use the given Strings.c and Strings.h module: Strings.c: #include "Strings.h" #include <string.h> #include<stdlib.h> #include<stdio.h> char* substring(char* str, int iPos){ if(iPos > strlen(str)||iPos < 0)return (char*)NULL; char* substr; substr = &str[iPos]; return substr; } int charPosition(char* str, char c){ char* string = (char*)malloc(strlen(str)+1); int i; for(i = 0; i < strlen(str)+1; i++) { if(str[i] == c) { return i; } } return -1; } Strings.h: #include <string.h> /* substring - return a pointer to the substring beginning at the iPos-th position....
(12) Explain what will be output of the following program? #include <stdio.h> #include <stdlib.h> #include <pthread.h>...
(12) Explain what will be output of the following program? #include <stdio.h> #include <stdlib.h> #include <pthread.h> #define NUM_THREADS 3 /* create thread argument struct for thr_func() */ typedef struct _thread_data_t {   int tid;   double stuff; } thread_data_t; /* thread function */ void *thr_func(void *arg) {   thread_data_t *data = (thread_data_t *)arg;   printf("hello from thr_func, thread id: %d\n", data->tid);   pthread_exit(NULL); } int main(int argc, char **argv) {   pthread_t thr[NUM_THREADS];   int i, rc;   thread_data_t thr_data[NUM_THREADS];   /* create threads */   for (i = 0;...
#include <stdio.h> #include <stdlib.h> #define K   1024 /** (2pts) * This problem is like p1, except...
#include <stdio.h> #include <stdlib.h> #define K   1024 /** (2pts) * This problem is like p1, except that you should read the number using scanf() * and the string to print using fgets() (up to 1024 characters.) Use "num: " as * the prompt for the number and "str: " as the prompt for the string. Keep in * mind that the newline that is entered will still be in the string when * printing it. NOTE: After the scanf() for...
#include <stdio.h> #include <stdlib.h> int play_game(int *); // Returns 0 if player won, 1 if the...
#include <stdio.h> #include <stdlib.h> int play_game(int *); // Returns 0 if player won, 1 if the computer won, 2 if there is a tie, and -1 if the player decides to quit int menu(int *); // Displays choices to user // Receives score array int main() { srand(42); // Seeding Random with 42 int score[3]; // Array keeping Player, Computer, and Tie Scores score [0] = 0; // Player - initialized to Zero score [1] = 0; // Computer -...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT