Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. (100 pts) Dowload the file assignbonus.c from class webpage and reduce the si

ID: 3818915 • Letter: 1

Question

1. (100 pts) Dowload the file assignbonus.c from class webpage and reduce the size of the file without changing the functionality. The two programs should do the same thing but the new file should be shorter. To find the number of characters in the file use the wc command as follows WC C assign bonus. C Original file has 895 characters in it. You can use the following techniques to reduce the size. Remove comments. Declare all variables of one type on one line. Reuse variables instead of declaring new variables. Use operators like t instead of using t and Other things that you can think of. Once you do all the reductions you can think of, look at the size of the executable file for the original file and new file and see how they differ Submit your program electronically using the blackboard system

Explanation / Answer

/*file reduced to 531 character(from original file being of 895 characters)*/
#include<stdio.h>
#include<math.h>
int main()
{
int i,n,sum=0;
float sum2=0;
printf("Enter n: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
sum=sum+i*i*i;
printf("For Loop Loop Result=%d Formula=%d ",sum,n*n*(n+1)*(n+1)/4);
i=1;
while(i<=n)
{
sum2=sum2+1.0/(pow(2.0,i));
i+=1;
}
printf("While Loop Loop Result=%f Formula=%f ",sum2,1-1.0/pow(2,n));
sum=0;
i=1;
do
{
sum=sum+i*(i+1);
i+=1;
}while(i<=n);
printf("Do while Loop Loop Result=%d Formula=%d ",sum,n*(n+1)*(n+2)/3);
return 0;
}

/*your original file*/
#include <stdio.h>
#include <math.h>

int main()
{
// Declares and initializes variables
int i,j, k;
int sum1, sum3;
float sum2;
int n;

// Read n from user  
printf("Enter n: ");
scanf("%d",&n);

// For loop
sum1 = 0;
for (k=1; k<=n; k++)
      sum1 = sum1 + k*k*k;

printf("For Loop ");
printf(" Loop Result = %d ",sum1);
printf(" Formula = %d ",n*n*(n+1)*(n+1)/4);

// while loop
sum2 = 0;
i = 1;
while (i<=n)
    {
      sum2 = sum2 + 1.0/(pow(2.0,i));
      i = i+1;
    }

printf("While Loop ");
printf(" Loop Result = %f ",sum2);
printf(" Formula = %f ", 1-1.0/pow(2,n));

// Do-while loop
sum3 = 0;
j = 1;
do
    {
      sum3 = sum3 + j*(j+1);
      j = j+1;
    } while (j<=n);

printf("Do while Loop ");
printf(" Loop Result = %d ",sum3);
printf(" Formula = %d ", n*(n+1)*(n+2)/3);


return 0;
}

/*program to check filesize*/
#include<stdio.h>
#include<conio.h>
void main()
{
    FILE *fp;
    char ch;
    int size = 0;

    fp = fopen("reduce.c", "r");/*put your file name here*/
    if (fp == NULL)
    {
        printf(" File unable to open ");
    }
    else
    {
        printf(" File opened ");
    }
    fseek(fp, 0, 2);    /* file pointer at the end of file */
    size = ftell(fp);   /* take a position of file pointer un size variable */
    printf("The size of given file is : %d ", size);  
    fclose(fp);
}