Skip to main content

2041013154 - Sarthak Mohanty (OSW 1)

*Q1 - Write a program that stores the values ’X’ and 76.1 in separate memory cells. Your program should get the values as data items and display them again for the user when done.*

#include <stdio.h>
int main()
{
char ch;
double num;
ch = 'X';
num = 76.1;
printf("The character is =%c\n", ch);
printf("The value is = %.lf", num);
}

Output:

➜  DSA-practice git:(main)cd "/home/cosmicsarthak/Documents/DSA-practice/Tests/c/" && gcc 1.c -o 1 && "/home/cosmicsarthak/Documents/DSA-practice/Tests/c/"1
The character is =X
The value is = 76

Q2 - Which of the following identifiers are (a) C reserved words, (b) standard identifiers, (c) conventionally used as constant macro names, (d) other valid identifiers, and (e) invalid identifiers?

  • void - Reserved word
  • MAX_ENTRIES - conventionally used as constant macro names,
  • return - Reserved word
  • pritnf - Standard Identifier
  • "char" - Invalid Identifier
  • xyz123 - Valid Identifier
  • time - valid Identifier
  • part#2 - Invalid Identifier
  • G - valid IdentifierS
  • Sue's - Invalid identifier
  • #insert - Invalid Identifier
  • this_is_a_long_one - Valid Identifier
  • double - Reserved Word
  • _hello_ - valid Identifier

Q3 - Write an assignment statement that might be used to implement the following equation in C.

Ans: q = (K*A(T1-T2))/L


Q4 - Write a program to that makes the following exchanges without using temporary variable;

Ans: The arrows indicate that b is assume the value of a, c the value of b and so on. Your program

must take input a, b and c from a data file mydata.txt and sends program output to output file

myoutput.txt using input-output redirection.

#include <stdio.h>
int main()
{
int a, b, c;
scanf("%d", &a);
scanf("%d", &b);
scanf("%d", &c);
printf("Before swap: a=%d b=%d c=%d", a, b, c);
a = a + b + c;
b = a - b - c;
c = a - b - c;
a = a - b - c;
printf("\nAfter swap: a=%d b=%d c=%d", a, b, c);
}

Output:

➜  DSA-practice git:(main) ✗ cd "/home/cosmicsarthak/Documents/DSA-practice/Tests/c/" && gcc 1.c -o 1 && "/home/cosmicsarthak/Documents/DSA-practice/Tests/c/"1
cd "/home/cosmicsarthak/Documents/DSA-practice/Tests/c/" && gcc 1.c -o 1 && "/home/cosmicsarthak/Documents/DSA-practice/Tests/c/"1
Before swap: a=32585 b=0 c=0
After swap: a=0 b=32585 c=0

Q5 - Write a program that calculates mileage reimbursement for a salesperson at a rate of \$0.35 per mile. Your program should interact with the user in this manner:

#include <stdio.h>
int main()
#define rate 0.35
{
printf("Mileage Reimbursement Calculator\n");
float odometer_init, odometer_fin, differnce, result;
printf("Enter beginning odometer reading => ");
scanf("%f", &odometer_init);
printf("Enter ending odometer reading => ");
scanf("%f", &odometer_fin);
differnce = odometer_fin - odometer_init;
result = differnce * rate;
printf("You traveled %.1f miles. At $%.2f per mile,\nyour reimursement is $ %.2f .\n", differnce, rate, result);
return 0;
}