Commit 18387716 by viakul

Upload New File

parent fa5dcbbb
Showing with 282 additions and 0 deletions
/**
* File: Homework_2.c
* Author: Viktor Akulcih
* Created: 26.11.2018
* Last edit: 26.11.2018
*
* Description: sorting, displaying the hiking traces to the user
*/
//inctlude libraries for user input/output and operation on strings
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//define the velocity
#define VELOCITY 5
//declaration of the functions that do diferents tasks
//the explanation about each function can be found before the function body
void filling_array(char arr1[15][20], char arr[15][20], float val[15], int n);
void sorting(char arr1[15][20], char arr2[15][20], float val[15], int n);
void tracePrint(char arr1[15][20], int n);
void printSort(char arr1[15][20], char arr2[15][20], int n);
void traceHaveTime(char arr[15][20], int n, int time, float val[15]);
int timeSet();
int firstVal();
int main()
{
int n, time;
/*the declration of integers first one is the number of
* traces second one is a time their are given by a user
*/
float val[15] = {0};
/*the values of distances of traces in float form
* we need this in order to compare which of the traces we
* can complete in given time span
*/
char trcs[15][20] = {0};
char dstnc[15][20] = {0};
// arrays for distances and names of traces
n = firstVal();
/* functin cheack whether n betwenn 1 and 15
* will return n only if it satisfies previous condition
* it gives the value for the number of traces
*/
filling_array(trcs, dstnc, val, n);
/*the function fills ask the user to
* fill the arrays of distance and name of traces
* Also converts distance into floats
*/
time = timeSet();
//ask user to print the value for time
tracePrint(trcs, n);
//prints all traces
sorting(trcs, dstnc, val, n);
//sorts the traces distances
printSort(trcs, dstnc, n);
//print sorted array of distances by the length
traceHaveTime(trcs, n, time, val);
/*writes the name of the traces that can be walked during
*the given time span and the give speed
*/
return 0;
}
/* The function ask the user to inser the value for time
* It will ask the user until it meets the requrenment
* (time should be positive)
*/
int timeSet(){
int time;
printf("Please write the time\n");
while(1)
{
scanf("%d", &time);
if(time > 0)
{
break;
}
else
{
printf("Please repeat\n");
}
}
return time;
}
/* The function's input:
* array of traces names, number of traces, the time and the distance values
* the function's does:
* prints all traces that user can complete in given period of time
* by comparing the product of time and velocity to the length of the
* trace. Since the array is already sorted it is possible to stop the
* comparison when the distanse is bigger than the user can walk
* if it meets the requirenment then it prints the accoriding trace
* otherwise it will break the loop.
*/
void traceHaveTime(char arr[15][20],int n, int time, float val[15]){
int i;
printf("One of the following trails can be completed in %d hour: ",time);
for(i = 0; i < n; i++)
{
if (time * VELOCITY >= val[i]){
printf("%s ", arr[i]);
}
else
{
break;
}
}
}
/* the function's input:
* two arrays the names
* the function's does:
* prints sorted value of trails and distanses
*/
void printSort(char arr1[15][20], char arr2[15][20], int n){
int i;
printf("Trail lengths\n");
for(i = 0; i < n; i++)
{
printf("%d.%s\t%s km\n", i + 1, arr1[i], arr2[i]);
//i+1 in order to start from 1 not from 0.
}
}
/* the function's input:
* unsorted array, and number of trails
* the function's does:
* prints all trails that were given by a user
*/
void tracePrint(char arr1[15][20], int n){
int i;
printf("Traces: ");
for(i = 0; i < n - 1; i++)
{
printf("%s, ", arr1[i]);
//does not print the last value since the last comma
//is unnecessary
}
printf("%s\n", arr1[n-1]);
//print the last value without a comma
}
/*function input:
* empty arrays of distances and names of traces,
* values for distances, number of traces;
* Since it is requred to know n traces and n distances
* the code reads the user input 2n times
*/
void filling_array(char arr[15][20],char arr1[15][20],float val[15],int n){
int i, y, j;
//use the variable to keep track of different elements inside arrays
printf("Please, start filling in the traces and distances\n");
y = 0;
j = 0;
for(i = 0; i < 2 * n; i++)
{
//first it scanf the value for trace then it scanf the value for
//distance, use additional variables j and y in order
// to keep track of number of element of each array
if(i % 2 == 0)
{
printf("Write the trace #%d:\n", j + 1);
scanf("%s",arr[j]);
j++;
}
else
{
printf("Write the distance #%d:\n", y + 1);
scanf("%s", arr1[y]);
val[y] = atof(arr1[y]);
//atof converts the string to a float
y++;
}
}
}
/*the function ask the user to inser the value
* It will ask the user until it meets the requrenment
*/
int firstVal(){
int n;
printf("Write the number of traces\n");
while(1)
{
printf("Your value n is: ");
scanf("%d",&n);
if((n < 1) || (n > 15))
{
printf("Please repeat the procedure\n");
}
else
{
break;
}
}
return n;
}
/*sorts the distances, names of traces by buble sort
* by comparint the value inside the val array to next one
*/
void sorting(char arr1[15][20], char arr2[15][20], float val[15], int n){
int i,j;
char temp[20];
float swap;
//swap and temp are used in order to swap array values
i = 0;
while(i < n)
{
j = 0;
while(j < n - 1)
{
if(val[j] > val[j+1])
{
swap = val[j];
val[j]=val[j+1];
val[j+1]=swap;
strcpy(temp,arr1[j]);
strcpy(arr1[j],arr1[j+1]);
strcpy(arr1[j+1],temp);
strcpy(temp,arr2[j]);
strcpy(arr2[j],arr2[j+1]);
strcpy(arr2[j+1],temp);
//swaping the values for all 3 arrays
j++;
}
else
{
j++;
}
}
i++;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment