Question 1.


[10 pts]  Give the output of the following program code:


int val;
for(val = -5; val < 5; val++)
{
  if( (val>0) && (val<3) )
    printf("alt1\n");
  else if( (val <= -2) || (val > 0) )
    printf("alt2\n");
  else
    printf("alt3\n");
} 

Answer:
------

alt2  /* Printed for -5 */

alt2  /* Printed for -4 */
 
alt2  /* Printed for -3 */

alt2  /* Printed for -2 */

alt3  /* Printed for -1 */

alt3  /* Printed for  0 */

alt1  /* Printed for +1 */

alt1  /* Printed for +2 */

alt2  /* Printed for +3 */

alt2  /* Printed for +4 */ 

Question 2



#define ARRAYSIZE 5

void arraytom(int a[],int M)
{
    int i;
    for(i=0;i<M;i++)
      a[i]=0;
}
 

int main()
{
  int array[ARRAYSIZE];
  arraytom(array,ARRAYSIZE);
}  

//Green 3.
/*--------------------*/

/*This is the main program */

#include>stdio.h>
#include
void print_triangle(int n);
int main()
{
	int n,i,j,k;
	printf("\n Enter the value of n :: ");
	scanf("%d",&n);
	while(n<2)
	{
		printf("\n Value should be greater than or equal to 2");
		printf("\n Enter the value of n :: ");
		scanf("%d",&n);
	}
	print_triangle(n);
	return 0;
}

/* This function prints the pair of triangles */
void print_triangle(int n)
{	
	int i,j,k;
	for(i=0;i<n;i++)
	{
		for(j=0;j>i+1;j++)
		{
			printf("*");
		}
		for(k=0;k<2;k++)
		        printf("  ");
		for(j=i+2;j<=n+1;j++)
		        printf("*");   
		printf("\n");

	}
}
/*--------------------*/

Question 4

Answer:
It helps you in knowing the exact logic of each part of the program.
Also it makes code easy to maintain. If we have to change some parts
of the program then documenting that will help other people to 
understand why exactly the change was made, when the change was
made etc. It also makes program easily readable and understandable.

Disadvantage of documenting your program code after you write
it is that you may forget what exactly is the logic of the 
program and may end up writing some thing wrong.

Question 5.

       0       1       2       3


0 98.876     1.0       ?       ?


1      ?       ?       ?       ?


2    687       ?       ?       ?


3      ?       ?   -10.7       ?


4      ?       ?       ?     5.6


/*A[2][4]*/ - column 4 does not exist.
/*A[1][1.1]*/ - float number given as index in the statement.
/*A[5][3]*/ - elment a[5][3] does not exist.(out of bounds)
/*A[-1][2]*/ - negative number given as index.
/*A['a'][1]*/ - ASCII value of 'a' is out of array bounds. Also: should not use
				characters to index into arrays!
 
Question 6 

[10 pts]  Write a recursive function that reads characters from the keyboard 
until a newline is read and then prints the characters in reverse order on 
the screen.  For example, if the user types the four characters "abcd" 
followed by a newline(carriage return), you should print to the screen: "dcba".

Answer:
------


#include 

void recursive()
{
  char ch;
  ch = getchar();
  if(ch != '\n')
    recursive();
  else
    return;
  printf("%c", ch);
}

int main()
{
  printf("Enter the word :\n");
  recursive();
  printf("\nPress return to finish.\n");
  getchar();
  return 0;
}


OUTPUT:

Enter the word :
abcd
dcba
Press return to finish.

Enter the word :
123asdf;lkj
jkl;fdsa321
Press return to finish.

Question 7

#include 

void FindPositionofSmallest(int *pos)
{   
  int currValue,ch,i = 1;
  FILE *fp;
      
  if((fp = fopen("data.txt","r")) == NULL)
  { 
    printf("\nError opening file.");
    exit(100);
  }

  if((fscanf(fp,"%d ",&currValue)) == EOF)
  { 
    printf("\n There aren't any integers to be read.");
    return;    
  }
  else
  {        
    *pos = 1;       
    while((fscanf(fp,"%d ",&ch)) == 1)
    {    
      i++;
      if( ch < currValue)
      { 
        currValue = ch;
        *pos = i;                 
      }            
    }
    fclose(fp);
    printf("\n\nThe position of the smallest integer in the given file is  %d", *pos);
    return ;
   }	    
}

int main( )
{ 
  int position;  
  FindPositionofSmallest(&position);  
  printf("\n\nThe position of the smallest integer in the given file is  %d ", position);
  fflush(stdin);
  printf("\n\nPress return to finish. ");
  getchar( );
  return 0;
}

Question 8

     No it will not always be better than the algorithms that take O(N^2) or 
O(NlogN).
This is because the complexity of an algorithm is defined  by it's Big-O notation 
eg. O(N) it means the function is of the form C1N + C2 + C3/N + C4/(N^2)....
for the polynomial function.Here there is an upperbound for C1

In the given situation O(N) is not always better than O(N^2) or O(NlogN) becuase the constant associated with O(N) may be huge compared to constant
compared associated with O(N^2) or O(NlogN).