Just Program It!




Hello there!

I'm still contemplating on what I did in the past examinations:
  • I passed the STAT1 First Long Exam but I scored somewhere on the modal score in the distribution of all scores of STAT1 students in this exam.
  • I passed the CMSC21 First Long Exam and I was part of the Top 10. (Yey!) It's not that easy to trace program codes!
  • I failed the CMSC57 First Long Exam and I am still trying to cope up to pass with the next examination involving Probability and Recurrence Relations
  • I'm doing it hard to pass the MATH27 First Long Exam this Monday. Practice those derivatives and integrals of transcendental functions!
  • I was just wondering if I did the right thing in drawing vectors in our PHYS3 Lab Exam...
  • I was not quite satisfied with the MST6 Long Exam... I wonder if I pass...


We just had our first On-the-Spot Programming exercise in our CMSC21 Lab. I was just getting there to type and compile all those codes to ensure I did it right.

These were our exercises in CMSC21 1st OTSP (from: Examination On-Line http://192.168.0.232:8082/EOL-V2/):

1. Write a program which has a function that swaps the values of 2 integers. Say a=7 and b = 8 after exiting the function the values should now be a=8, b=7

My answer:

#include < stdio.h >
#include < conio.h >
void swap_int(int *, int *);
main(){
int a, b; clrscr();
printf("Enter first integer:");
scanf("%d", &a);
printf("Enter second integer:");
scanf("%d", &b);
printf("Before swapping: A=%d ; B=%d", a, b);
swap_int(&a, &b);
printf(" After swapping: A=%d ; B=%d", a, b);
getch();
return 0;
}
void swap_int(int *a, int *b){
int temp;
temp = *a;
*a = *b;
*b = temp;
}

The correct answer is somewhat like this:

#include < stdio.h >
int swap(int *x,int *b);
main() {
int a,b; printf ("a: ");
scanf("%d", &a );
printf ("b: ");
scanf("%d", &b );
printf("a=%d, b=%d", a,b);
swap(&a,&b);
printf("a=%d, b=%d", a,b);
return 0; }
int swap(int *a, int*b) {
int temp;
temp = *a;
*a=*b;
*b=temp;
}

Ok... I got it right. Twenty points for me.

2. Write a program that adds 2 polynomials with highest degree 5. in a term 4x2 coefficient is 4 and exponent is 2.

To input the polynomial 2x3+ 5x2 +4x+1: [program prompt]:[user input}
Coefficient of term with exponent 5: 0
Coefficient of term with exponent 4: 0
Coefficient of term with exponent 3: 2
Coefficient of term with exponent 2: 5
Coefficient of term with exponent 1: 4
Coefficient of term with exponent 0: 1

Your program should follow the following output---
Input the values for the 2 polynomials.
Polynomial 1:
Coefficient of term with exponent 5: 0

Coefficient of term with exponent 4: 0
Coefficient of term with exponent 3: 2
Coefficient of term with exponent 2: 5
Coefficient of term with exponent 1: 4
Coefficient of term with exponent 0: 1
Polynomial 2:
Coefficient of term with exponent 5: 3
Coefficient of term with exponent 4: 2
Coefficient of term with exponent 3: 4
Coefficient of term with exponent 2: 0
Coefficient of term with exponent 1: 0
Coefficient of term with exponent 0: 8

The sum of the 2 polynomials is: 3x5 + 2x4 + 6x3 + 5x2 + 4x + 9



My Answer:
#include < stdio.h >
#include < conio.h >
void add_poly(int a[], int b[]);
main(){
clrscr();
int coeff_A[6],coeff_B[6];
printf("Input the Values for the 2 Polynomials:");
printf(" Polynomial 1:");
printf(" Coefficient of term with degree 5:");
scanf("%d", &coeff_A[0]);
printf(" Coefficient of term with degree 4:");
scanf("%d", &coeff_A[1]);
printf(" Coefficient of term with degree 3:");
scanf("%d", &coeff_A[2]);
printf(" Coefficient of term with degree 2:");
scanf("%d", &coeff_A[3]);
printf(" Coeff. of degree 1:");
scanf("%d", &coeff_A[4]);
printf(" Coefficient of term with degree 0:");
scanf("%d", &coeff_A[5]);
printf(" Polynomial 2:");
printf(" Coefficient of term with degree 5:");
scanf("%d", &coeff_B[0]);
printf(" CCoefficient of term with degree 4:");
scanf("%d", &coeff_B[1]);
printf(" Coefficient of term with degree 3:");
scanf("%d", &coeff_B[2]);
printf(" Coefficient of term with degree 2:");
scanf("%d", &coeff_B[3]);
printf(" Coefficient of term with degree 1:");
scanf("%d", &coeff_B[4]);
printf(" Coefficient of term with degree 0:");
scanf("%d", &coeff_B[5]); a
dd_poly(coeff_A, coeff_B);
getch();
return 0;
}
void add_poly(int coeff_1[6], int coeff_2[6]){
int i,new_coeff[6];
for(i=0;i<6;i++){>
new_coeff[i]= coeff_1[i] + coeff_2[i];
}
printf(" The sum of the 2 polynomials is:");
printf(" %dx^5 + %dx^4 + %dx^3 + %dx^2 + %dx + %d", new_coeff[0], new_coeff[1], new_coeff[2], new_coeff[3], new_coeff[4], new_coeff[5]); }



Correct Answer: The following is just one possible answer:

#include < stdio.h >
/* function prototypes */
int SIZE = 5;
void addpoly ( int poly1[], int poly2[], int poly3[]);
main() {
int i;
int pA[] ={0,0,0,0,0,0};
int pB[] ={0,0,0,0,0,0};
int pC[] ={0,0,0,0,0,0};
clrscr();
printf (" Input the values for the 2 polynomials");
printf (" Polynomial 1");
for (i=SIZE; i>=0; i--) {
printf (" Coefficient of term with exponent %d: ", i);
scanf("%d",&pA[i]);
}
printf (" Polynomial 2");
for (i=SIZE; i>=0; i--){
printf (" Coefficient of term with exponent %d: ", i);
scanf("%d",&pB[i]);
}
addpoly(pA, pB, pC);
printf (" The sum of the 2 polynomials is: ");
if (pC[SIZE]!=0)
printf (" %d x %d ",pC[SIZE], SIZE);
for (i=SIZE-1; i>1; i--) {
if (pC[i]!=0) {
if (pC[i] > 0)
printf("+"); printf (" %d x %d",pC[i], i); }
}
if (pC[i]!=0) {
if (pC[i] > 0)
printf("+");
printf (" %d x ",pC[i--]); }
if (pC[i]!=0) {
if (pC[i] > 0)
printf("+");
printf ("%d ",pC[i]);
}
return 0;
}

void addpoly ( int poly1[], int poly2[], int poly3[]) {
int ctr = 0;
for (ctr = 0; ctr <= SIZE; ctr ++ ) { poly3[ctr] = poly2[ctr] + poly1[ctr]; } }


This is what our lab teacher, Prof. Ramoran, commented on my mistake:
you can use a loop to ask the coefficients. you were on the right track when you created a function in adding the polynomials however you should pass the sum(which is also a polynomial) back to main
Ok. 25 points out of 30. Not bad.



It's just not easy to program in C...

See you around!

KENNETH

Digg!

Add to Technorati Favorites
Personal Blogs - Blog Catalog Blog Directory




Subscribe to 'A Stellar Life' Feeds Today!

Comments

Popular posts from this blog

LOOK AT THIS!: Venta5, A Great TV Shopping Hit

ASL Q&A: Pagsangkap

Freedom From Lies With Hatred: Contamination From Deceitful Content Against The Church And How I Dealt It With The Truth