Question
Problem 2. Write a program that reads a list of integers from the file 2k. txt. You may assume that there are no more than 2,500 numbers in the file. Read "keys" from the file keys. txt -store them in an array. You may assume that there are no more than 20 keys. You may start with assignment5q2_template.c.

Write the following functions:
(a) Find a given key in the un-sorted array (as inputed). Return the position at which the key was found (return - -1 if the key was not in the array).
(b) Write a function that will sort a given array (you may use any algorithm). Note, duplicates are kept in the array.
Solution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references.
Students may use these solutions for personal skill-building and practice.
Unethical use is strictly forbidden.

#include <stdio.h>
#include <stdlib.h>

#define MAX_LIST 2500
#define MAX_KEYS 20

// can be used for debuging

void print(int arr[], int sz){
    for(int i = 0; i < sz; i++){
       printf("%d ", arr[i]);
    }
    printf("\n");
}


void read_int_arr(char *filename, int arr[], int *n){
    FILE *fp;
    fp = fopen(filename, "r");
    if(!fp){
       printf("Could not open file %s\n", filename);
       exit(1);
    }
    *n = 0;
    while(fscanf(fp, "%d", &arr[*n]) == 1){
       (*n)++;
    }
    fclose(fp);
}

int position(int * list, int size, int key){
    int i;
    // iterate from the first item to the last item
    for (i = 0; i < size; i++) {
       if (list[i] == key) { // this item meets key value
            // return its index
            return i;
       }
This is only a preview of the solution.
Please use the purchase button to see the entire solution.
By purchasing this solution you'll be able to access the following files:
Solution.zip
Purchase Solution
$15.00 $7.5
Google Pay
Amazon
Paypal
Mastercard
Visacard
Discover
Amex
View Available Computer Science Tutors 639 tutors matched
Ionut
(ionut)
Master of Computer Science
Hi! MSc Applied Informatics & Computer Science Engineer. Practical experience in many CS & IT branches.Research work & homework
5/5 (6,804+ sessions)
1 hour avg response
$15-$50 hourly rate
Pranay
(math1983)
Doctor of Philosophy (PhD)
Ph.D. in mathematics and working as an Assistant Professor in University. I can provide help in mathematics, statistics and allied areas.
4.6/5 (6,670+ sessions)
1 hour avg response
$40-$50 hourly rate
Leo
(Leo)
Doctor of Philosophy (PhD)
Hi! I have been a professor in New York and taught in a math department and in an applied math department.
4.9/5 (6,425+ sessions)
2 hours avg response

Similar Homework Solutions