Skip to content
Open
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
18 changes: 18 additions & 0 deletions Assignment 8 Sheqi
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Assignment 8 Sheqi

1. What is the output? Assume that the malloc() call was successful. If you think that the program contains bugs (other than memory leaks) that can make the program crash (ex. segmentation fault), write "CAN CRASH" and then write the most likely output if the program happens to run without crashing.
Answer:
The output is "ABCabc123". CAN CRASH. Most likely output: usage: ./a.out <arg1> <arg2>.

2. If the program is run using valgrind, how many bytes will valgrind report as "definitely lost"? You can write 0 if you think there is no memory leak that valgrind will consider "definitely lost". Please make sure to write the number of BYTES.
Answer:
1*2=2. Because only *p is been allocated, while is not freed as well. In this circumstance, only *p is the memory leak.

3. Identify the line numbers that contains memory errors other than memory leaks (invalid access, for example.) If you think that there is no memory error other than possible memory leaks, write NONE. You don’t have to identify the nature of the memory errors. Just line numbers.
Answer:
None.

4. Modify the program to fix ALL memory errors, including memory leaks. Write only the lines that need to be fixed. Write the line number and the line of code that will replace that line. If you think there is nothing to fix, write "NO CHANGE". The program can be memory error free by modifying 0 to 3 lines of code. Please do not modify more than 3 lines of code.
35 free(p);
36 return 0;
37}
41 changes: 41 additions & 0 deletions meanStandardDeviation.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main(){
FILE*numberFile = fopen("number.txt","r");

char number[100];
fgets(number,sizeof(number),numberFile);
int a,i;

for(i=0;i<sizeof(number);i++){
a = atof(number);
fgets(number,100,numberFile);
}

float sum;

for(i=0;i<sizeof(number);i++){
sum = sum + number[i];
return sum;
}

float mean= sum/sizeof(number);

float sumOD;

for(i=0;i<sizeof(number);i++){
sumOD= sumOD+(number[i]-mean)*(number[i]-mean);
return sumOD;
}

float square= sumOD/(sizeof(number)-1);
float s= sqrt(square);

printf("%f\n",s);

fclose(numberFile);

return 0;
}
81 changes: 81 additions & 0 deletions sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int main(){
srand(time(NULL));
printf("Please enter an integer:\n");
int x;
scanf("%d",&x);
int array1[x];
int*p=array1;
p=(int*) malloc (100);

int i;
for(i=0;i<x;i++){
array1[i]=rand()%100;
// printf("%d\n",array1[i]);
}

int array2[x];
int array3[x];
int*q=array2;
q=(int*) malloc(100);
int*b=array3;
b=(int*) malloc(100);

for(i=0;i<x;i++){
array2[i]=array1[i];
array3[i]=array1[i];
// printf("%d\n",array2[i]);
}

int j; //www.studystreet.com/c-program-sort-array-ascending-order/
int tem1=0;
for(i=0;i<x;i++)
{
for(j=0;j<x-i;j++)
{
if(array2[j]>array2[j+1])
{
tem1=array2[j];
array2[j]=array2[j+1];
array2[j+1]=tem1;
}
}
}

int d; //www.studystreet.com/program-sort-array-descending-order/
int tem2=0;
for(i=0;i<x;i++)
{
for(d=0;d<x-i;d++)
{
if(array3[d]<array3[d+1])
{
tem2=array3[d+1];
array3[d+1]=array3[d];
array3[d]=tem2;
}
}
}

printf("array1 is:\n");
for(i=0;i<x;i++){
printf("%d\n",array1[i]);
}

printf("array2 is:\n");
for(i=0;i<x;i++){
printf("%d\n",array2[i]);
}

printf("array3 is:\n");
for(i=0;i<x;i++){
printf("%d\n",array3[i]);
}

return 0;
}