Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sdvar #3

Merged
merged 2 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 48 additions & 3 deletions console.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Console input and output.
// Input is from the keyboard or serial port.
// Output is written to the screen and serial port.

#include "types.h"
#include "defs.h"
#include "param.h"
Expand Down Expand Up @@ -187,6 +186,9 @@ struct {
} input;

#define C(x) ((x)-'@') // Control-x
#define K 4
char saved_buf[INPUT_BUF]; // saved buffer of copy-paste
int saved_buffer_size = 0;

void
consoleintr(int (*getc)(void))
Expand All @@ -200,6 +202,51 @@ consoleintr(int (*getc)(void))
// procdump() locks cons.lock indirectly; invoke later
doprocdump = 1;
break;
case 67: case 88: //copy and cut
for (int i = (input.e - input.r) - K; i < input.e - input.r ; i++)
{
saved_buf[i - (input.e - input.r - K)] = input.buf[(input.r + i ) % INPUT_BUF];
saved_buffer_size += 1;
}

if (c == 88)
{
while(input.e != input.w && input.buf[(input.e-1) % INPUT_BUF] != '\n'){
input.e--;
consputc(BACKSPACE);
}
}
break;
case 86: //paste
for (int i = 0; i < saved_buffer_size; i++)
{
c = saved_buf[i];
consputc(c);
}
break;

case C('E'):
for (int i = 0; i < input.e - input.r ; i++)
{
int temp = input.buf[(input.r + i ) % INPUT_BUF];
if(temp >= 48 && temp <= 53)
{
temp += 4;
}
else if(temp >= 54 && temp <= 57)
{

}
else if(temp >= 97 && temp <= 122)
{

}
else if(temp >= 65 && temp <= 90)
{

}
// saved_buf[i] =
}
case C('U'): // Kill line.
while(input.e != input.w &&
input.buf[(input.e-1) % INPUT_BUF] != '\n'){
Expand Down Expand Up @@ -293,7 +340,5 @@ consoleinit(void)
devsw[CONSOLE].write = consolewrite;
devsw[CONSOLE].read = consoleread;
cons.locking = 1;

ioapicenable(IRQ_KBD, 0);
}

8 changes: 4 additions & 4 deletions init.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ main(void)
printf(1, "init: starting sh\n");

// printing students name and id
printf(1, "Group #8 ");
printf(1, "Ali Parvizi - 810100102");
printf(1, "Mohammad Mataee - 810199493");
printf(1, "Mohammad Javad Afsari - 810198544");
printf(1, "Group #8 \n");
printf(1, "Ali Parvizi - 810100102 \n");
printf(1, "Mohammad Mataee - 810199493 \n");
printf(1, "Mohammad Javad Afsari - 810198544 \n");

pid = fork();
if(pid < 0){
Expand Down
202 changes: 142 additions & 60 deletions sdvar.c
Original file line number Diff line number Diff line change
@@ -1,76 +1,158 @@
#include "types.h"
#include "stat.h"
#include "user.h"
#include "fcntl.h"

#define MAX_NUMBERS 7

float calculateSD(double data[],int n) {
float sum = 0.0, mean, SD = 0.0;
int i;
for (i = 0; i < n; ++i) {
sum += data[i];
int abs(int x) {
return x >= 0 ? x : -x;
}

double sqrt(double x) {
if (x < 0) return -1;

double epsilon = 0.1;
double guess = x;

while (abs(guess * guess - x) >= epsilon) {
guess = (guess + x / guess) / 2.0;
}
mean = sum / n;
for (i = 0; i < n; ++i) {
SD += pow(data[i] - mean, 2);

return guess;
}

int calculate_sum(int arr[], int size) {
int sum = 0;
for(int i = 0; i < size; i++){
sum += arr[i];
}
return sqrt(SD / n);
return sum;
}
float Variance(double a[], int n)

double calculate_mean(int arr[], int size)
{
int sum = 0;
for (int i = 0; i < n; i++)
sum += a[i];
double mean = (double)sum / (double)n;

double sqDiff = 0;
for (int i = 0; i < n; i++)
sqDiff += (a[i] - mean) *
(a[i] - mean);
return sqDiff / n;
return (double)calculate_sum(arr, size) / size;
}
int main() {
double numbers[MAX_NUMBERS];
int count = 0;
double sum = 0.0;

// Read input numbers
printf("Enter up to %d numbers (separated by spaces):\n", MAX_NUMBERS);
while (count < MAX_NUMBERS && scanf("%lf", &numbers[count]) == 1) {
sum += numbers[count];
count++;

double calculate_variance(int arr[], int n) {
double sum_of_squared_differences = 0.0;
double mean = calculate_mean(arr, n);

for (int i = 0; i < n; i++) {
double difference = (double)arr[i] - mean;
sum_of_squared_differences += difference * difference;
}

// Calculate average
int average = sum / count;
// Calculate standard deviation and variance
double standard_deviation_array[MAX_NUMBERS];
double variance_array[MAX_NUMBERS];
int sda_temp = 0;
int va_temp = 0;
for(int i=0; i<count; i++)
{
if(numbers[i] <= average)
{

standard_deviation_array[sda_temp] = numbers[i];
sda_temp++;
return sum_of_squared_differences / n;
}

double calculate_standard_deviation(int arr[], int n) {
return sqrt(calculate_variance(arr, n));
}

void initializeBiggerAndSmallerNumbers(int numbers[], int size , int mean, int* smallNumbers, int* bigNumbers){
int j = 0;
int k = 0;
for (int i = 0; i < size; i++) {
if (numbers[i] <= mean) {
smallNumbers[j++] = numbers[i];
} else {
bigNumbers[k++] = numbers[i];
}
else
{
variance_array[va_temp] = numbers[i];
va_temp++;
}
}

void reverse(char s[]) {
int i, j;
char c;
for (i = 0, j = strlen(s)-1; i < j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}

void itoa(int n, char s[]) {
int i, sign;
if ((sign = n) < 0) {
n = -n; // Make n positive
}
i = 0;
do {
s[i++] = n % 10 + '0'; // Get next digit
} while ((n /= 10) > 0); // Delete it
if (sign < 0) {
s[i++] = '-';
}
s[i] = '\0'; // Null-terminate the string
reverse(s);
}


int main(int argc, char* argv[]) {

if (argc > 8){
printf(1, "Error! : size of array is not valid");
exit();
}

int numbers[MAX_NUMBERS];
int size = argc - 1;

for (int i = 0; i < size; i++){
numbers[i] = atoi(argv[i + 1]);
}

int mean = (int)calculate_mean(numbers, size);


int smallCount = 0;
int bigCount = 0;
for (int i = 0; i < size; i++) {
if (numbers[i] <= mean) {
smallCount++;
} else {
bigCount++;
}
}
int standard_deviation = calculateSD(standard_deviation_array, sda_temp);
int variance = Variance(variance_array, va_temp);
// Save results to a file
FILE *file = fopen("txt.result_sdvar", "w");
if (file != NULL) {
fprintf(file, "%d\n", average);
fprintf(file, "%d\n", standard_deviation);
fprintf(file, "%d\n", variance);
fclose(file);

int* smallNumbers = (int*)malloc(smallCount * sizeof(int));
int* bigNumbers = (int*)malloc(bigCount * sizeof(int));


initializeBiggerAndSmallerNumbers(numbers, size, mean, smallNumbers, bigNumbers);

int sd = (int)calculate_standard_deviation(smallNumbers, smallCount);
int variance = (int)calculate_variance(bigNumbers, bigCount);

free(bigNumbers);
free(smallNumbers);


unlink("strdiff_result.txt");
int fd = open("sdvar_result.txt", O_CREATE | O_WRONLY);
if (fd >= 0) {

char mean_str[10];
char sd_str[10];
char variance_str[10];

itoa(mean, mean_str);
itoa(sd, sd_str);
itoa(variance, variance_str);

write(fd, mean_str, strlen(mean_str));
write(fd, " ", 1);
write(fd, sd_str, strlen(sd_str));
write(fd, " ", 1);
write(fd, variance_str, strlen(variance_str));
write(fd, "\n", 1);

close(fd);
} else {
printf("Error opening file for writing.\n");
printf(1, "Error opening file for writing.\n");
}

return 0;
exit();
}