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

## Instructions

Objective
Write a program to sort the states by rate of all violent crimes in C language.

## Requirements and Specifications

Source Code

```/*selection_sort.c, project 5, Program Design  */ #include <stdio.h> #include <string.h> #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<n; 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; }```