[go: up one dir, main page]

0% found this document useful (0 votes)
35 views7 pages

UNIX Assignment .

The document describes an assignment to write C++ programs. It involves writing 5 correct and 5 incorrect programs, creating scripts to compile and execute the programs, and displaying the output files containing the compilation errors and program outputs.

Uploaded by

devpratapbth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views7 pages

UNIX Assignment .

The document describes an assignment to write C++ programs. It involves writing 5 correct and 5 incorrect programs, creating scripts to compile and execute the programs, and displaying the output files containing the compilation errors and program outputs.

Uploaded by

devpratapbth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

ASSIGNMENT

Name:- Dev Pratap Lal


Enrollment No:- A45304822071
Sec:- B
Course:- BCA-III
Subject:- UNIX OS and Shell Programming Given
By:- Prof. Avishek Choudhari

1. Write 5 correct C++ program in separate directory named programs.

dev@dev-VirtualBox:~/assignment$ cat rcode1.c


#include <stdio.h> int
main() {
printf("Hello, World!\n");
return 0;
}

dev@dev-VirtualBox:~/assignment$ cat rcode2.c


#include <stdio.h>

int main() { int n


= 5, sum = 0;

for (int i = 1; i <= n; ++i) {


sum += i;
}

printf("Sum of first %d natural numbers: %d\n", n, sum);


return 0;
}

dev@dev-VirtualBox:~/assignment$ cat rcode3.c


#include <stdio.h>

int factorial(int n) {
if (n == 0 || n == 1)
return 1; else
return n * factorial(n - 1);
}

int main() {
int num = 5;
printf("Factorial of %d: %d\n", num, factorial(num));
return 0;
}
dev@dev-VirtualBox:~/assignment$ cat rcode4.c
#include <stdio.h>

int main() {
int n = 10, first = 0, second = 1, next;

printf("Fibonacci Series: ");

for (int i = 1; i <= n; ++i) {


printf("%d, ", first);
next = first + second;
first = second;
second = next;
}

printf("\n");
return 0;
}
dev@dev-VirtualBox:~/assignment$ cat rcode5.c

#include <stdio.h> int


isPrime(int num) {
if (num <= 1)
return 0;

for (int i = 2; i <= num / 2; ++i) {


if (num % i == 0)
return 0;
}

return 1;
}

int main() {
int num = 7;

if (isPrime(num))
printf("%d is a prime number.\n", num);
else
printf("%d is not a prime number.\n", num);

return 0;
}
2. Write 5 C++ programs which has been created above with some errors in same
directory.

dev@dev-VirtualBox:~/assignment$ cat wcode1.c


include <stdio.h> int add(int a, int b) {
return a + b;
}
int main() { int result =
add(3, 4); printf("Sum:
%d\n", result);
return 0;
}
dev@dev-VirtualBox:~/assignment$ cat wcode2.c

# <stdio.h> int
main() { int i = 1;
while (i <= 5) {
printf("%d ", i);
i++;
}
printf("\n");
return 0;
}
dev@dev-VirtualBox:~/assignment$ cat wcode3.c
#include <stdio.h> int
main() {
for ( i = 1; i <= 5; i++) {
printf("%d ", i);
}
printf("\n");
return 0;
}
dev@dev-VirtualBox:~/assignment$ cat wcode4.c
#include <stdio.h> int main() {
int num=7; printf("Entered
number is: ");
if (num 0) {
printf("Positive\n");
} else (num < 0) {
printf("Negative\n");
} else {
printf("Zero\n");
}
return 0;
}
dev@dev-VirtualBox:~/assignment$ cat wcode5.c

#include <stdio.h> int


main() {
int a = 5, b ;
printf("Sum: %d\n", a + b);
printf("Difference: %d\n", a - b);
printf("Product: %d\n", a b);
printf("Quotient: %d\n", a / b);
return 0;
}
dev@dev-VirtualBox:~/assignment$ ls

a.out execute.sh rcode1.c rcode3 rcode4.c wcode1.c wcode4.c


rcode2 rcode3.c rcode5 wcode2.c wcode5.c rcode1 rcode2.c
rcode4 rcode5.c wcode3.c.
Write a script which contains command for compiling all the programs with .c
extension and redirect the error generated in another text file.

dev@dev-VirtualBox:~/assignment$ cat compile.sh


#!/bin/bash
# Directory where the .c files are located
source_directory="/home/dev/assignment"
# Change to the source directory cd
"$source_directory" || exit 1
# Output directory for the log file (same as the source directory)
output_directory="$source_directory" # Log file to
capture compilation errors
log_file="$output_directory/compilation_errors.txt
"
# Clear the log file
> "$log_file"
# Compile all .c files in the source
directory for c_file in *.c; do if [ -f
"$c_file" ]; then
filename_noext="${c_file%.*}"
# Redirect both stdout and stderr to the log file
g++ -o "$filename_noext" "$c_file" 2>> "$log_file"
if [ $? -eq 0 ]; then
echo "Compiled: $c_file" else echo
"Compilation failed for: $c_file" fi fi
done
# Move the log file to the source directory if not already
there if [ "$output_directory" != "$source_directory" ]; then
mv "$log_file" "$source_directory" fi
echo "Compilation complete. Compilation errors are logged in
$source_directory/compilation_errors.txt"

5. Display the text file after execution of script which contains compilation errors.

dev@dev-VirtualBox:~/assignment$ cat compilation_errors.txt


wcode1.c:1:1: error: ‘include’ does not name a type
1 | include <stdio.h>
| ^~~ wcode1.c: In function
‘int main()’:
wcode1.c:6:18: error: ‘add’ was not declared in this scope
6 | int result = add(3, 4);
| ^~~
wcode1.c:7:5: error: ‘printf’ was not declared in this scope
7 | printf("Sum: %d\n", result);
| ^~~~
wcode1.c:1:1: note: ‘printf’ is defined in header ‘<cstdio>’; did you forget to ‘#include
<cstdio>’? +++ |+#include <cstdio> 1 | include <stdio.h>
wcode2.c:1:3: error: invalid preprocessing directive #<
1 | # <stdio.h>
| ^
wcode2.c: In function ‘int main()’:
wcode2.c:5:9: error: ‘printf’ was not declared in this scope
5| printf("%d ", i);
| ^~~~
wcode2.c:1:1: note: ‘printf’ is defined in header ‘<cstdio>’; did you forget to ‘#include <cstdio>’?
+++ |+#include <cstdio>
1 | # <stdio.h>
wcode2.c:8:5: error: ‘printf’ was not declared in this scope
8 | printf("\n");
| ^~~~
wcode2.c:8:5: note: ‘printf’ is defined in header ‘<cstdio>’; did you forget to ‘#include <cstdio>’?
wcode3.c: In function ‘int main()’:
wcode3.c:3:11: error: ‘i’ was not declared in this scope
3 | for ( i = 1; i <= 5; i++) {
| ^
wcode4.c: In function ‘int main()’:
wcode4.c:5:12: error: expected ‘)’ before numeric constant
5 | if (num 0) {
| ~ ^~
| )
wcode4.c:7:22: error: expected ‘;’ before ‘{’ token
7 | } else (num < 0) {
| ^~
| ;
wcode4.c:9:7: error: ‘else’ without a previous ‘if’
9 | } else {
| ^~
wcode5.c: In function ‘int main()’: wcode5.c:6:30:
error: expected ‘)’ before ‘b’
6 | printf("Product: %d\n", a b);
| ~ ^~
| )
6. Write one another script which contains command to execute all the programs with
extension of .c and redirect their output in another text file.

dev@dev-VirtualBox:~/assignment$ cat execute.sh

#!/bin/bash

# Directory where the .cpp files are located

source_directory="/home/dev/assignment"

# Change to the source directory cd

"$source_directory" || exit 1

# Output directory for the log file (same as the source directory)

output_directory="$source_directory"

# Log file to capture program outputs

output_file="$output_directory/program_outputs.txt"

# Clear the output file

> "$output_file"

# Execute all .c files in the source

directory for c_file in *.c; do if [ -f


"$c_file" ]; then

filename_noext="${c_file%.*}"

# Execute the program and redirect its output to the log file

./"$filename_noext" >> "$output_file" 2>&1

echo "Executed: $c_file"

fi

done echo "Execution complete. Output is

logged in

$source_directory/program_outputs.txt"

7. Display the text file after execution of script which contains output.

dev@dev-VirtualBox:~/assignment$ ls
a.out execute.sh rcode1.c rcode3 rcode4.c wcode1.c wcode4.c
compilation_errors.txt program_outputs.txt rcode2 rcode3.c rcode5 wcode2.c wcode5.c
compile.sh rcode1 rcode2.c rcode4 rcode5.c wcode3.c
dev@dev-VirtualBox:~/assignment$ cat program_outputs.txt Hello,
World!
Sum of first 5 natural numbers: 15
Factorial of 5: 120
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 7
is a prime number.
./execute.sh: line 17: ./wcode1: No such file or directory
./execute.sh: line 17: ./wcode2: No such file or directory
./execute.sh: line 17: ./wcode3: No such file or directory
./execute.sh: line 17: ./wcode4: No such file or directory
./execute.sh: line 17: ./wcode5: No such file or directory

You might also like