From fc99b515d988cbb365af150ec3d83a3529319071 Mon Sep 17 00:00:00 2001 From: Ankit Kumar Date: Thu, 27 Feb 2025 18:30:58 +0530 Subject: [PATCH 1/5] fixed the logical errors and formated the code for better readeability --- issue1.c | 23 +++++++++++++---------- issue2.c | 10 ++++++---- issue3.c | 16 +++++++++------- issue4.c | 24 +++++++++++++++--------- issue5.c | 54 ++++++++++++++++++++++++++++++++---------------------- issue6.c | 15 ++++++++------- issue7.c | 13 +++++++------ issue8.c | 16 +++++++++------- issue9.c | 4 ++-- 9 files changed, 101 insertions(+), 74 deletions(-) diff --git a/issue1.c b/issue1.c index 05a1df5..ac79cf7 100644 --- a/issue1.c +++ b/issue1.c @@ -1,20 +1,23 @@ -// Make sure the function reverse actually reverses the string +// Made sure the function reverse actually reverses the string -#include -#include -#include +#include +#include +#include -char* reverse(char* str){ - char* rev = (char*)malloc(sizeof(str)); +char *reverse(char *str) +{ + char *rev = (char *)malloc(sizeof(str)); int size = strlen(str); - for(int i=0;i -int main(){ +int main() +{ int even = 0; - for(int i = 0; i <= 20; i++){ - even *= 2; + for (int i = 1; i <= 20; i++) + { + even += 2; printf("%d\t", even); } printf("\n"); diff --git a/issue3.c b/issue3.c index 7a9f3df..ad832d3 100644 --- a/issue3.c +++ b/issue3.c @@ -1,21 +1,23 @@ -// Make sure the program prints the string correctly +// Made sure the program prints the string correctly #include -int main(){ +int main() +{ // Printing all the characters in an array char z[] = {'h', 'e', 'l', 'l', 'o', '!'}; - char new[50] = ""; + char new[50] = ""; - for(int i = 0; i < 6; i++){ - new[i] = z; + for (int i = 0; i < 6; i++) + { + new[i] = z[i]; } - for(int i = 0; i < 6; i++){ + for (int i = 0; i < 6; i++) + { printf("%c", new[i]); } printf("\n"); return 0; - } \ No newline at end of file diff --git a/issue4.c b/issue4.c index 5fe7765..6421193 100644 --- a/issue4.c +++ b/issue4.c @@ -1,11 +1,15 @@ -// Make sure the sorting algorithm is correct +// Made sure the sorting algorithm is correct #include -void sort(int a[], int b){ - for(int i = 0; i < b; i++){ - for(int j = 1; j < b; j++){ - if(a[i] > a[j]){ +void ascending_sort(int a[], int b) +{ + for (int i = 0; i < b; i++) + { + for (int j = i; j < b; j++) + { + if (a[i] > a[j]) + { int temp = a[i]; a[i] = a[j]; a[j] = temp; @@ -14,12 +18,14 @@ void sort(int a[], int b){ } } -int main(){ +int main() +{ // Sort in ascending order int numbers[] = {4, 2, 3, 1, 8, 7}; - sort(numbers, 6); - - for(int i = 0; i < 6; i++){ + ascending_sort(numbers, 6); + + for (int i = 0; i < 6; i++) + { printf("%d", numbers[i]); } } \ No newline at end of file diff --git a/issue5.c b/issue5.c index 2ff3dd5..2d8225a 100644 --- a/issue5.c +++ b/issue5.c @@ -1,29 +1,39 @@ -// Make sure you only get the desired output +// Made sure you only get the desired output -#include -int main(void){ +#include +int main(void) +{ int flag = 0; - + printf("Choose the operation:-\n1. Multiples of 2\n2. Multiples of 3\n3. Multiples of 4\n"); scanf(" %d", &flag); - switch(flag){ - case 1 : - for(int i=1;i<6;i++){ - printf("%d\t", 2*i); - } - printf("\n"); - case 2 : - for(int i=1;i<6;i++){ - printf("%d\t", 3*i); - } - printf("\n"); - case 3 : - for(int i=1;i<6;i++){ - printf("%d\t", 4*i); - } - printf("\n"); - default : - printf("Choose a valid option!!!\n"); + switch (flag) + { + case 1: + for (int i = 1; i < 6; i++) + { + printf("%d\t", 2 * i); + } + printf("\n"); + break; + case 2: + for (int i = 1; i < 6; i++) + { + printf("%d\t", 3 * i); + } + printf("\n"); + break; + case 3: + for (int i = 1; i < 6; i++) + { + printf("%d\t", 4 * i); + } + printf("\n"); + break; + + default: + printf("Choose a valid option!!!\n"); + break; } } \ No newline at end of file diff --git a/issue6.c b/issue6.c index 94a0656..37e7080 100644 --- a/issue6.c +++ b/issue6.c @@ -1,12 +1,13 @@ -// Make sure the program is working correct logically +// Made sure the program is working correct logically -#include -#include -int main(void){ - char* str1 = "This is a string"; - char* str2 = "This is another string"; +#include +#include +int main(void) +{ + char *str1 = "This is a string"; + char *str2 = "This is another string"; - if(strcmp(str1, str2)) + if (!strcmp(str1, str2)) printf("Strings are same.\n"); else printf("Strings are not same.\n"); diff --git a/issue7.c b/issue7.c index fc3d230..b6b0b3c 100644 --- a/issue7.c +++ b/issue7.c @@ -1,11 +1,12 @@ -// Make sure the output is the correct sum of a and b +// Made sure the output is the correct sum of a and b -#include -#include +#include +#include -int main(void){ +int main(void) +{ int a = 5; - int* b = (int*)malloc(sizeof(int)); + int *b = (int *)malloc(sizeof(int)); *b = 7; - printf("%d\n", (a+b)); + printf("%d\n", (a + *b)); } \ No newline at end of file diff --git a/issue8.c b/issue8.c index 4876a9b..bf9fc66 100644 --- a/issue8.c +++ b/issue8.c @@ -1,11 +1,13 @@ -// Only print the given string, nothing more -// Bonus if you don't count the number of characters in the string manually +// Only printed the given string, nothing more +// didn't count the number of characters in the string manually -#include -#include -int main(void){ - char* str = "Welcome to IEEE\n"; - for(int i=0;i<100;i++){ +#include +#include +int main(void) +{ + char *str = "Welcome to IEEE\n"; + for (int i = 0; i < strlen(str) - 1; i++) + { printf("%c", str[i]); } printf("\n"); diff --git a/issue9.c b/issue9.c index f28b571..f985ab6 100644 --- a/issue9.c +++ b/issue9.c @@ -1,8 +1,8 @@ -// print correct factorial of the variable "num" +// printed correct factorial of the variable "num" #include int factorial(int n){ - if(n == 2) + if(n == 1) return 1; else return n*factorial(n-1); From ce13931d8221e929cbdf29b803bf58bc35805db0 Mon Sep 17 00:00:00 2001 From: Ank-Frost Date: Sat, 12 Apr 2025 12:46:45 +0530 Subject: [PATCH 2/5] MIni Task 1: using /proc to get system uptime and loadavg --- proc.sh | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 proc.sh diff --git a/proc.sh b/proc.sh new file mode 100644 index 0000000..5196dce --- /dev/null +++ b/proc.sh @@ -0,0 +1,24 @@ +#! /bin/bash + +# showinf uptime +a=($(cat /proc/uptime)) +h=$( echo "${a[0]}/3600" | bc ) +min=$(echo "((${a[0]}%3600)/60)" | bc ) + +echo "uptime: $h hr and $min min" + +# showing load average +l=($(cat /proc/loadavg)) +echo "loadavg" +for num in {0..2} +do +c=$( echo "${l[num]}*100" | bc ) + +echo " $c % " +done +echo " running processes/total : ${l[3]} " +echo " most recent process (ID) : ${l[4]} " + + + + From f1c9b2ba912e0be8453a3d74b6472b5cd555415d Mon Sep 17 00:00:00 2001 From: Ank-Frost Date: Mon, 12 May 2025 02:01:28 +0530 Subject: [PATCH 3/5] made a simple terminal ui yo display current running processes using ncurses --- .vscode/settings.json | 3 ++ __pycache__/matplotlib.cpython-312.pyc | Bin 0 -> 498 bytes a.out | Bin 0 -> 71544 bytes makefile | 3 ++ matplotlib.py | 9 ++++ ncurses.cpp | 53 +++++++++++++++++++++ process_view.c | 61 +++++++++++++++++++++++++ 7 files changed, 129 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 __pycache__/matplotlib.cpython-312.pyc create mode 100755 a.out create mode 100644 makefile create mode 100644 matplotlib.py create mode 100644 ncurses.cpp create mode 100644 process_view.c diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..6e3efe2 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "C_Cpp.default.compilerPath": "/bin/gcc" +} \ No newline at end of file diff --git a/__pycache__/matplotlib.cpython-312.pyc b/__pycache__/matplotlib.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..c0243a493f0c3d371e767ed179b8043773e4a4c2 GIT binary patch literal 498 zcmX@j%ge<81TudWGOB>|V-N=hSfGs08-R@I3@HpLj5!Rsj8ROC4CxFhOp6$!n4vTa z6GJLf7T6@1W(b|ioXWDA3BqP%0IT9bRRQ9qFt27rQNxEt4GWqY0ZcWkp+bxdm5iFK zFF`y%P4<@{vtNQ7@sb%tfV}mR6-2Os2zDTGi?QMsW2GkNEy3Kxl7gK4lAO#Wy@E;* z>lPc7W-2Ht0{O6r8>E2+B3F@(quvlM6Km#<{_3{=7J%Sb;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bZ@qUc{ zH$(BrGuUq_B>n{IF};8~em*4rBI(ik=iuuh@i3Hj+#T^EY{`IWTB)4rS|hM!98k`o zoVN0LbIi7mn!^Rs3Zyx1*{uu@$J3Cp9eX5Kc5TOWN6mD;Sg^}jCsQ;>^2L;uH#2V0 zDVtUW3l>L9dE2!!@z%x>=5Vf%GcCulCQQ5FIukJLSfjR?sf>L^miRFKO1k2@7T9*-;UOuyI{2!uT;Q7Q>maI0E0wo# z{3$#~zo-f63ZgCB?n0qPqf8YYthNwS+nJdjw+OtI&mFht$GGDm7#p$ObT%x_&RDJm z-W5Yub~;-Oi}Xfp7czJmO2v}BzybT9t5{Bljcv@ahaI~dHipq-VzbJ_DV{FTkm;A1RojpowWYY}VtHE=P2IIgRsxLh`PM*LqS`VL2rHhkUiHvZ1Q zmcP@fW-peH$=;7UeS+eA{vN@03k7K#K9*{>J?QEA{JWEFtR|Y?!E3ku=>8{u`{Mpj zs{^+D9g)8m^7l5b(vJu9(~t8nr~|h6Q`p<+`yw53KL65wOzHFasw4hx!rvdgBKSwW zMz9Nigh%vkkMsGv3fqK=FrIz3Z(s(+-=|7Y58 q=kfD>N#9X;?0WvWWquEr0~3z> +using namespace std; + +int main() +{ + // initating a terminal window + initscr(); + // to start reciveing input immideateliy but on by default + cbreak(); + // to pritnt to window + printw("hello world"); + printw("enter the pin:"); + // to hide the keystrokes + noecho(); + for (int i = 0; i < 4; i++) + { + getch(); + } + // to show the keystrokes + echo(); + + int height = 10, width = 20, start_y = 1, start_x = 1; + + // created a window + WINDOW *win = newwin(height, width, start_y, start_x); + //to refresh the window + refresh(); + + //to add border to created sub window + box(win, 0, 0); + mvwprintw(win, 1, 1, "frame 1"); + wrefresh(win); + + + // getting environment info + int y, x, ybeg, xbeg, ymax, xmax; + getyx(stdscr, y, x); + getmaxyx(stdscr, ymax, xmax); + getbegyx(win, ybeg, xbeg); + + move(10, 10); + box(win, 10, 10); + + wrefresh(win); + + printw("%d %d %d %d %d %d", y, x, ybeg, xbeg, ymax, xmax); + wrefresh(win); + getch(); + + endwin(); + + return 0; +} \ No newline at end of file diff --git a/process_view.c b/process_view.c new file mode 100644 index 0000000..ec31ec0 --- /dev/null +++ b/process_view.c @@ -0,0 +1,61 @@ +#include +#include +#include +#include + +#define MAX_LINE 1024 + +void draw_processes(WINDOW *win) { + FILE *fp; + char line[MAX_LINE]; + int row = 1; + + werase(win); // Clear window + box(win, 0, 0); + mvwprintw(win, 0, 2, " Process Viewer(Press E to Exit)"); + mvwprintw(win, 1, 2, "PID TTY TIME CMD"); + + fp = popen("ps -a", "r"); + if (fp == NULL) { + mvwprintw(win, 2, 2, "Failed to run ps command"); + wrefresh(win); + return; + } + + fgets(line, sizeof(line), fp); // Skip the header line + + while (fgets(line, sizeof(line), fp) != NULL && row < LINES - 2) { + mvwprintw(win, ++row, 2, "%.*s", COLS - 4, line); // prevent overflow + } + + pclose(fp); + wrefresh(win); +} + +int main() { + initscr(); + noecho(); + cbreak(); + curs_set(0); + nodelay(stdscr, TRUE); // Don't block on getch() + keypad(stdscr, TRUE); // Enable function keys + + WINDOW *win = newwin(LINES, COLS, 0, 0); + int ch; + + while (1) { + draw_processes(win); + + ch = getch(); // Non-blocking key check + if (ch == 'E' || ch == 'e') { + break; + } + + usleep(2000000); // Refresh every 2 seconds + } + + delwin(win); + endwin(); + return 0; +} + From b95fe443610b5fa34b7a00fc18bf97127386bb52 Mon Sep 17 00:00:00 2001 From: Ankit Kumar Date: Mon, 12 May 2025 02:14:44 +0530 Subject: [PATCH 4/5] Rename ncurses.cpp to ncurses.c --- ncurses.cpp => ncurses.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename ncurses.cpp => ncurses.c (99%) diff --git a/ncurses.cpp b/ncurses.c similarity index 99% rename from ncurses.cpp rename to ncurses.c index edca5a0..248afb0 100644 --- a/ncurses.cpp +++ b/ncurses.c @@ -50,4 +50,4 @@ int main() endwin(); return 0; -} \ No newline at end of file +} From e6dbcc06b98c7e95a0ecacdea9f2df661ec21e5a Mon Sep 17 00:00:00 2001 From: Ankit Kumar Date: Mon, 12 May 2025 02:35:39 +0530 Subject: [PATCH 5/5] Update ncurses.c --- ncurses.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ncurses.c b/ncurses.c index 248afb0..8a9f739 100644 --- a/ncurses.c +++ b/ncurses.c @@ -1,5 +1,5 @@ #include -using namespace std; + int main() {