Saturday 27 February 2016

NPTEL(WEEK-5.4)

Programming Assignment 5.4: Number in a position

Due on 2016-02-27, 23:55 IST

You are given a sequence of integers. The length of the sequence is unknown but the sequence ends with a -1. You are given another number pos . Your task is to output the integer in position pos. If pos > length of the sequence output -1.

You are supposed to implement two functions :
Node loadNum(): Function to load the numbers onto the linked list      Return a pointer to the head of the list
void releaseMem(Node head): Function to release the memory after every iteration
Note: pos is indexed from 1.

Input
T
a11 a12 .. -1
Pos 1
aT1  aT2 … -1
Pos T

Output
N1
N2
..
..
NT

Constraints
1 <= T <= 30
1 <= aij <= 100
There is no restriction on the length of each list.

Example
Input
1
4 3 2 1 -1
2

Output
3


PROGRAMMING:

#include <stdio.h>
#include <stdlib.h>

struct node{
    int data;
    struct node* next;
};

typedef struct node* Node;

/*    Function to load the numbers onto the linked list
      Return a pointer to the head of the list  */
Node loadNum();

/*    Function to print the number in position pos
      head is guaranteed to be non-NULL */
/*    Function to print the number in position pos
      head is guaranteed to be non-NULL */
void printNum(Node head,int pos)
{
    int i = 1;
    Node temp = head;
    while( i != pos ){
     temp = temp->next;
     if( temp == NULL){
      printf("-1");
      return;    
     }
     i++;
    }
    printf("%d",temp->data);
}


/* Function to release the memory after every iteration */
void releaseMem(Node head);


int main()
{
    int i,T;
    int pos;
    Node head;
    scanf("%d",&T);
    for (i = 0; i < T; i++){

    head = loadNum();
    scanf("%d",&pos);
    printNum(head, pos);
        if(i<T-1)
          printf("\n"); // Will add a new line for after all output
                        // except for last.
    releaseMem(head);
    }
    return 0;
}


CODING:


/*    Function to load the numbers onto the linked list
      Return a pointer to the head of the list  */
Node loadNum()
{
  Node head,temp;
  int t;
  head=(struct node *)malloc(sizeof(struct node));
  scanf("%d",&t);
  head->data=t;
  head->next=NULL;
  temp=head;
  scanf("%d",&t);
  do
  {
    temp=head;
    Node var=(struct node *)malloc(sizeof(struct node));
    var->data=t;
    while(temp->next!=NULL)
         {
              temp=temp->next;
         }
         var->next=NULL;
         temp->next=var;
    scanf("%d",&t);

  }while(t!=-1);
  return head;
}

/* Function to release the memory after every iteration */
void releaseMem(Node head)
{

}




Monday 22 February 2016

NPTEL (WEEK-5.3)

Programming Assignment 5.3: String Class

Due on 2016-02-27, 23:55 IST

Implement a custom string class with public functions

1) combine which takes as argument two strings s1, s2 and appends s2 to s1.
2) replace which takes as argument two characters, and replaces all occurrences of the first character by the second character in the string, if the first character is found, otherwise replace returns NOT FOUND.

In the main program take two strings as input (on first line), then take two characters as input.  Concatenate the two strings using combine.   Replace the first character in the concatenated string (if found) with the second character.

Implement following public functions for the  String Class.

  1. String String::operator+ (String str) : Create an new String object and copy the base String and then append content of String str. 
  2. void String::replace (char oldChar, char newChar) : Replace the "oldChar" if found with "newChar"
  3. char * String::getCharArray(): Return a Character array with content of String.
  4. bool String::find (char c): Return true if the character is present in the string else false.

Input:

The first two line contains two strings, string1 and string2. The second line contains two characters char1 and char2.

The two strings will contain only letters in English alphabet (both upper and lower case). The two characters will also be English letters.

Output:

First concatenate the two strings. In the result, all occurrences of char1 should be replaced with char2 and that should be printed out.If char1 is not present in the resultant string after concatenation, print NOT FOUND

PROGRAM:

/**********************************************************
 * String Class
 * CONCEPTS: Operator overloading,dynamic memory allocation.
 **********************************************************/
//#include<iostream>
#include<cstdio>
#define MAXLEN 100

using namespace std;

class String {                                                                                                                              
   private:                                                                                                                                    
      char *s;                                                                                                                                  
      int size;                                                                                                                                 
   public:                                                                                                                                     
       String();                                                                                                                                 
       String(char *str);                                                       //Create a String class from a character array                                                                                                                   
       int len();                                                                    //Return length of string                                                                                                                  
      String operator+ (String str);                                     //Return a new string with appending 2 strings         (this+str)                                                                                 
      void operator= (char *str);                                     //Assign a new string from char[]str                                                                                                      
      void replace (char oldChar, char newChar);            //replace all occurrences of oldChar by     newChar                                                                                             
      bool find (char c);                                                    //Is `c` is found in this string                                                                                                          
      char * getCharArray();                                            // return a char array with content of string                                                                                             
};


String::String() {
 s = NULL;
 size = 0;
}

String::String(char *str) {
  int len = 0;

  while(str[len]!='\0') {
    len++;
  }
  size = len;
  s = new char[len];
  
  for(int i = 0; i <=size; i++){
    s[i]=str[i];
  }
}

int String::len() {
  return size; 
}
void String::operator= (char *str) {
  
  int len = 0;
  int i = 0;
  
  while(str[len]!='\0') len++;
  size = len;
  
  if( s != NULL) delete []s;

  s = new char[len];
  
  for(int i = 0; i <= size; i++){
    s[i]=str[i];
  }
}


int main() {
  String s1,s2;
  char inputStr[MAXLEN];
  
  scanf("%s",inputStr);
  s1 = inputStr;

  scanf("%s",inputStr);
  s2 = inputStr;
  
  String s3 = s1 + s2;
  char oldChar,newChar;
  scanf(" %c %c",&oldChar, &newChar);
  if( s3.find(oldChar) ) {
    s3.replace(oldChar, newChar);
      printf("%s",s3.getCharArray());
  } else {
      printf("NOT FOUND");
  }
  return 0;





CODING :

String String::operator+ (String str){
    String newStr;
    newStr.s = new char[size+str.size];
  int i,j;
  for(i=0;i<size;i++)
    {
  newStr.s[i] = s[i];
  newStr.s[i+1] = '\0';
    }
  for(j=0;j<str.size;i++,j++)
    {
      newStr.s[i] = str.s[j];
      newStr.s[i+1] = '\0';
    }

    //printf("\nAfter :%s",newStr.s);
    return newStr;
}


void String::replace (char oldChar, char newChar) {
  int f=0,i=0,l=0;

   while(s[l]!='\0') l++;

  for(i=0;i<l;i++)
  {
    if(s[i]==oldChar)
        s[i]=newChar;
  }

}

bool String::find (char c) {
  int f=0,i,l=0;

   while(s[l]!='\0') l++;

  for(i=0;i<l;i++)
  {
    if(s[i]==c)
    {
      f=1;
      break;
    }
  }
  if(f==0)
        return false;
    else
        return true;
}

char * String::getCharArray(){
    int i=0,l=0;
        while(s[l]!='\0') l++;
  char * str = new char[l+1];
  for(i=0;i<l;i++)
  {
     str[i] = s[i];
  }
  return str;
}




NPTEL(WEEK-5.2)

Programming Assignment 5.2: Matrix ADT

Due on 2016-02-27, 23:55 IST



You have to implement a matrix ADT using concepts of C++ classes taught in the lectures. The input matrices would be square matrices. The  class must support the following functions:

1. Matrix Addition

2. Matrix Subtraction

3. Matrix Multiplication
The program should take as input: dimension of a square matrix N, two matrices of size N x N with integer values, and one operator symbol (+, - ,*). It must perform the corresponding operation using member functions of Matrix class. You are supposed to implement the following functions of the class MatrixADT matrixType add(matrixType M1, matrixType M2): The function should add matrices M1 and M2 and store the result in "resultMatrix". The function should return the "resultMatrix".
matrixType subtract(matrixType M1, matrixType M2):
The function should subtract matrix M2 from M1 and store the result in "resultMatrix". The function should return the "resultMatrix".
matrixType multiply(matrixType M1, matrixType M2):
The function should multiply matrices M1 and M2 and store the result in "resultMatrix". Be careful, it is M1*M2 and not M2*M1. The function should return the "resultMatrix".

INPUT:
In the first line, one integer which is the dimension of the matrix  and one operator (one of +, - or *)
Two NxN matrices one after the other, supplied one row at a time.
OUTPUT:
Resultant matrix after performing the specified operation, one row at a time. For subtraction, if A is the first matrix and B is the second matrix, perform A-B.
CONSTRAINTS:
The inputs will satisfy the following constraints:
1<=N<=10
There is no need to validate the value of N.
There are no constraints on the values of the entries of the matrices.



PROGRAM :


#include <cstring>

#include <cstdio>

using namespace std;

struct matrixType{

    int matDimension;
    int matValues[10][10];
};
class MatrixADT{
    private:
        matrixType resultMatrix;
    public:
       
        //Member function declarations        
        void intializeResultMatrix(int);
        matrixType add(matrixType, matrixType);
        matrixType subtract(matrixType,matrixType);
        matrixType multiply(matrixType,matrixType);
        void printResult();
};

//Intialize Result Matrix entries to zero
void MatrixADT::intializeResultMatrix(int dim){
        int i,j;
        
        resultMatrix.matDimension = dim;
        for (i=0;i<resultMatrix.matDimension;i++){
                for (j=0; j<resultMatrix.matDimension;j++){
                        resultMatrix.matValues[i][j] = 0;
                }
        }
}

CODING:

matrixType MatrixADT::add(matrixType M1, matrixType M2){
  int i=0,j=0;
  for (i=0;i<resultMatrix.matDimension;i++)
        {
            for (j=0; j<resultMatrix.matDimension;j++){
                resultMatrix.matValues[i][j] = M1.matValues[i][j] + M2.matValues[i][j];
            }
        }


        return resultMatrix;
}
//SUBTRACT
matrixType MatrixADT::subtract(matrixType M1, matrixType M2)
{
    int i=0,j=0;
  for (i=0;i<resultMatrix.matDimension;i++)
        {
            for (j=0; j<resultMatrix.matDimension;j++){
                resultMatrix.matValues[i][j] = M1.matValues[i][j] - M2.matValues[i][j];
        }
        }
    return resultMatrix;
}
//MULTIPLY
matrixType MatrixADT::multiply(matrixType M1, matrixType M2){
    int i=0,j=0,ii,jj;
  for (i=0;i<resultMatrix.matDimension;i++)
        {
            for (j=0; j<resultMatrix.matDimension;j++)
            {
                for(ii=0;ii<resultMatrix.matDimension;ii++)
                {
                    for(jj=0;jj<resultMatrix.matDimension;jj++)
                    {
                        if(j==ii)
                        {
                             resultMatrix.matValues[i][jj] += M1.matValues[i][j] * M2.matValues[ii][jj];
                        }
                    }
                }
            }
        }
        return resultMatrix;
}



PROGRAM :

int main(){ MatrixADT maX; matrixType M1, M2; char op; int i,j,dim; scanf("%d %c",&dim, &op); M1.matDimension = dim; M2.matDimension = dim; maX.intializeResultMatrix(dim); //Accept matrix entries for (i=0;i<dim;i++) for (j=0; j<dim;j++){ scanf("%d",&M1.matValues[i][j]); } for (i=0;i<dim;i++) for (j=0; j<dim;j++){ scanf("%d",&M2.matValues[i][j]); } //Call MatrixADT member function depending on operator if (op=='+'){ maX.add(M1,M2); } else if (op=='-'){ maX.subtract(M1,M2); } else{ maX.multiply(M1,M2); } maX.printResult(); return 0; } void MatrixADT::printResult(){ int i,j; for (i=0;i<resultMatrix.matDimension;i++){ for (j=0; j<resultMatrix.matDimension-1;j++){ printf("%d ",resultMatrix.matValues[i][j]); } printf("%d\n",resultMatrix.matValues[i][j]); } printf("Done"); }

Friday 19 February 2016

NPTEL(WEEK-5.1)

Programming Assignment 5.1: Topper and Average


Given a list of student names and their  marks in K subjects,  find the average mark for every student. Also, print a list of toppers in each subject. (Assume there is only one  topper).
Use the given structure Student (in C language). You should the  implement following functions which are called in main():
  • char * getTopper(struct Student s[], int nStudents, int subjectIndex, int nSubjects);
/*
Return name of topper of a subject.
Arguments :
array of Student records.
number of Students (nStudents).
index of subject for which function should find toper (subjectIndex).
number of Subjects (nSubjects)
*/
  • float getAvg(struct Student  s, int nSubjects);
/*
return avg mark of a student.
Arguments :
a Student record s.
number of Subjects (nSubjects)
*/
  • void setValues(struct Student* s, const char * name, float marks[], int nSubjects);
/*
set values in structure Student
Arguments :
a pointer to a Student record (s).
name of Student (name).
mark of K subjects (marks).
number of Subjects (nSubjects)
*/

Input
:
Given a list of student names and their marks in K subjects, find the average mark for every student. Also, print a list of toppers in each subject. (Assume there is only one topper).

The first line contains the number of students (N) and the number of subjects (K). Following N lines contains name of student and the marks of K subjects.

Output
The first N lines of output should contain average marks of each student (Print 6 decimal places) . Following K lines should contain the name of the topper of each subject in the order in which the subjects are given.

Constraints
1 <= N <=100
1 <= K <=10
Assume that Name does not contain spaces and the maximum length of names of the students is 10 (excluding the NULL character at the end).
0 <= Mark <=100 and the marks are real numbers.




Program:

#include<stdio.h>
#include<string.h>
#define N 100
#define K 10

struct Student {
  char name[10];
  float marks[10];
};

char * getTopper(struct Student [], int nStudents, int subjectIndex, int nSubjects);
/* return name of topper of a subject*/
float getAvg(struct Student , int nSubjects);
/* return avg mark of student s*/
void setValues(struct Student* , const char * name, float marks[], int nSubjects);

/* set values in struct Student s */


Coding :

char * getTopper(struct Student s[], int nStudents, int subjectIndex, int nSubjects)
{
    int i=0,id=0;
    float max=s[0].marks[subjectIndex],temp;
    for(i=0;i<nStudents;i++)
    {
        if(s[i].marks[subjectIndex]>max)
        {
            id = i;
            max = s[i].marks[subjectIndex];
        }
    }
    return (s[id].name);
}

float getAvg(struct Student  s, int nSubjects)
{
    int i;
    float avg=0;
    for(i=0;i<nSubjects;i++)
    {
        avg += s.marks[i];
    }
    avg = avg / nSubjects;
    return avg;
}
void setValues(struct Student* s, const char * name, float marks[], int nSubjects)
{
    int i=0;
    strcpy(s->name,name);
    for(i=0;i<nSubjects;i++)
    {
        s->marks[i] = marks[i];
    }
}

Program :

int main()
{
  struct Student students[N];
  int nStudents, nSubjects;
  int i, j;
  char name[10];
  float marks[K];
  /*Read Student data*/
  scanf("%d %d", &nStudents, &nSubjects);
  for (i = 0; i < nStudents; ++i) {
scanf("%s", name);
for (j = 0; j < nSubjects; ++j) {
  scanf("%f", &marks[j]);
}
setValues(&(students[i]), name, marks, nSubjects);
  }
  for (i = 0; i < nStudents; ++i) {
printf("%f\n", getAvg(students[i], nSubjects));
  }
  for (j = 0; j < nSubjects; ++j) {
if( j == nSubjects -1)
    printf("%s", getTopper(students, nStudents, j, nSubjects));
else
     printf("%s\n", getTopper(students, nStudents, j, nSubjects));
  }
  return 0;

}

How to install google-chrome in redhat without redhat subscription

Install google-chrome in redhat  Download the .rpm file of chrome https://www.google.com/chrome/thank-you.html?installdataindex=empty&st...