+1 (315) 557-6473 

Program To Sort the States by Rate of All Violent Crimes in C Assignment Solutions.


Instructions

Objective
Write a C assignment program to sort the states by rate of all violent crimes in C language. This program aims to take a list of states along with their corresponding rates of violent crimes and arrange them in ascending or descending order based on the crime rates. By implementing sorting algorithms like bubble sort, insertion sort, or quicksort in C, you can effectively organize the states' data and present it in a more manageable format. Such a C assignment not only reinforces your understanding of programming concepts but also equips you with the skills to work with real-world data and develop efficient algorithms.

Requirements and Specifications

Program to sort the states by rate of all violent crimes in C language
Program to sort the states by rate of all violent crimes in C language 1

Source Code

/*selection_sort.c, project 5, Program Design

 */

#include

#include

#define MAX_SIZE 100

#define MAX_LENGTH_S 256

typedef struct state {

  char name[MAX_LENGTH_S];

  int population;

  double assaultRate;

  double murderRate;

  double rapeRate;

  double robberyRate;

  double totalRate;

} state;

void selection_sort(state list[], int n) {

 int i, largest = 0;

 state temp;

 if (n == 1) {

    return;

 }

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

    if (list[i].totalRate < list[largest].totalRate)

      largest = i;

  if (largest < n - 1) {

    state temp = list[n - 1];

    list[n - 1] = list[largest];

    list[largest] = temp;

  }

  selection_sort(list, n - 1);

}

int main(void) {

  int i, n;

  state data[MAX_SIZE];

  char filename[MAX_LENGTH_S];

  char out_filename[MAX_LENGTH_S] = {'s', 'o', 'r', 't', 'e', 'd', '_', 0};

  FILE *f;

  printf("Enter the file name: ");

  i = scanf("%s", filename);

  f = fopen(filename, "r");

  if (!f) {

    printf("Can not open file %s\n", filename);

    return 1;

  }

  i = 0;

  while (fscanf(f, "%[^,], %d, %lf, %lf, %lf, %lf\n", data[i].name,

                &data[i].population, &data[i].murderRate, &data[i].murderRate,

                &data[i].rapeRate, &data[i].robberyRate) != EOF) {

    data[i].totalRate = data[i].assaultRate + data[i].murderRate +

                        data[i].rapeRate + data[i].robberyRate;

    i++;

  }

 fclose(f);

  n = i;

 selection_sort(data, n);

 strcat(out_filename, filename);

  f = fopen(out_filename, "w");

  if (!f) {

    printf("Can not open file %s\n", out_filename);

    return 1;

  }

 for(i = 0; i

  fprintf(f, "%s, %d, %lf, %lf, %lf, %lf\n", data[i].name,

                data[i].population, data[i].murderRate, data[i].murderRate,

                data[i].rapeRate, data[i].robberyRate);

 }

 fclose(f);

  return 0;

}