2017 Fall CIS200 Lab 7 Release date: November 1. 2017 Due date: November 8, 2017
ID: 3607328 • Letter: 2
Question
2017 Fall CIS200 Lab 7 Release date: November 1. 2017 Due date: November 8, 2017 Submission based on Turn-In template Note: you also t so need to perform file exist and empty file cheeks in your programs maximray of type as an input parameter and if you want the minimum or ele is process you Program 1: Let A be an a of typholdsalues n array of n elements. Write a template function, minMaxFunc(...), which takes an um value. Using TWO stacks to determine the minimum or maximum value and return that stack I (LINKED LIST) holds values in ascending/descending order, stack 2 (ARRAY) porary" work stack as you insert values to keep them in order). Once all values have been rted the class T. t POP. Enthis question, you may hardcode n-5. You must write all functions you call (PUSH, stored in a data file (data.dat): 4 1 1332 1.1 4.1 8.1 5.2 2.3 ciss you must also print the values in the stack. Assume the operatorsExplanation / Answer
Code:
-----------
#include <iostream>
#include <fstream>
#include <typeinfo>
#include <string.h>
#include <stdlib.h>
using namespace std;
/* Defining Template */
template<typename T>
/* Sorting function (uses bubble sort)
* inputs:
* Array of elements
* Length of the Array
* */
void sortArray(T a[], int n)
{
int i, j;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
T element;
element = a[i];
a[i] = a[j];
a[j] = element;
}
}
}
}
int main()
{
/* Initialization */
float fArray[1024];
int iArray[1024];
char cArray[1024];
int typeFlag = 0;
FILE * fp;
FILE *fout;
char * line = NULL;
size_t len = 0;
ssize_t read;
/* Open input and output files */
fp = fopen("data.dat", "r");
fout = fopen("out.txt", "w");
if ((fp == NULL) || (fout == NULL))
return 0;
/* Read data from the file line by line */
while ((read = getline(&line, &len, fp)) != -1) {
char* token;
/* Split the line based on a delimetor space " " */
token = strtok(line, " ");
int count = 0;
/* Fills tokens into arrays */
while (token != NULL)
{
/* Fills into character array */
if (isalpha(token[0]))
{
typeFlag = 1;
cArray[count] = token[0];
count = count + 1;
}
else if (iswdigit(token[0]))
{
/* Fills into float array */
if (strchr(token, '.') != NULL)
{
typeFlag = 2;
fArray[count] = atof(token);
count = count + 1;
}
/* Fills into integer array */
else
{
typeFlag = 3;
iArray[count] = atoi(token);
count = count + 1;
}
}
token = strtok(NULL, " ");
}
/* Sorting the arrays and writing the sorted array into output file */
if (typeFlag == 1)
{
sortArray(cArray,count);
for(int i=0;i<count;i++)
{
fprintf(fout, "%c ", cArray[i]);
}
}
else if (typeFlag == 2)
{
sortArray(fArray,count);
for(int i=0;i<count;i++)
{
fprintf(fout, "%f ", fArray[i]);
}
}
else if (typeFlag == 3)
{
sortArray(iArray, count);
for(int i=0;i<count;i++)
{
fprintf(fout, "%d ", iArray[i]);
}
}
fprintf(fout, " ");
}
fclose(fp);
fclose(fout);
return 0;
}
Input file format (data.dat): (Note: each line should contain same type of data)
----------------------------------
1 3 4 0 1 2 9 10 4 2
1.1 3.4 8.9 0.4 0.1 3.2 4.5
a b e z d y c
Output file after execution (out.txt)
------------------------------------------------
0 1 1 2 2 3 4 4 9 10
0.000000 0.0=100000 0.400000 1.100000 3.200000 3.400000 4.500000 8.900000
a b c d e y z
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.