Code with Vinay

ISC practical papers › ISC 2008 › Question 2

Histogram & Statistics

ISC 2008 Computer Science Practical, Question 2 · Arrays and matrices

The question

A sentence is terminated by either “ . ” , “ ! ” or “ ? ” followed by a space. Input a piece of text consisting of sentences. Assume that there will be a maximum of 10 sentences in block letters.

Write a program to: (i) Obtain the length of the sentence (measured in words) and the frequency of vowels in each sentence.

(ii) Generate the output as shown below using the given data.

Sample data:

INPUT:  HELLO! HOW ARE YOU? HOPE EVERYTHING IS FINE. BEST OF LUCK.
OUTPUT
Sentence     No. of Vowels     No. of words
 1          2               1
 2          5               3
 3          8               4
 4          3               3
Sentence     No. of vowels / words
1         VVVVVV
          WWW
2         VVVVVVVVVVVVVVV
          WWWWWWWWW
3         VVVVVVVVVVVVVVVVVVVVVVVV
          WWWWWWWWWWWW
4         VVVVVVVVV
          WWWWWWWWW

Scale used 1:3

Question: ISC 2008 Computer Science Paper 2 (Practical), Question 2, set by the Council for the Indian School Certificate Examinations (CISCE). Solution and video: Vinay Singh.

Java solution

ISC2008Q2.java Download GitHub
import java.util.*;
class Text{
    private String sentence[];
    static String vowels="aeiouAEIOU";
    private int word[], vowel[], max;
    public Text(String paragraph){
        sentence = paragraph.split("[.,?!]");
        word=new int[sentence.length];
        vowel=new int[sentence.length];

        System.out.println(Arrays.toString(sentence));
        computeStats();
    }
    private static boolean isVowel(char ch){
        return vowels.indexOf(ch)>=0;
    }
    private void computeStats(){
        char ch;
        String temp[];
        max=0;
        sentence[0]=" "+sentence[0];
        for(int i=0; i<sentence.length;i++){
            for(int j=0; j<sentence[i].length();j++){
                ch=sentence[i].charAt(j);
                if(ch==' ') word[i]++;
                if(isVowel(ch)) vowel[i]++;
            }
            if(vowel[i]>max) max=vowel[i];
            if(word[i]>max)max = word[i];
        }
    }
    public void showStats(){
        System.out.println("Sentence\tNo. of Vowels\tNo. of words");
        for(int i=0; i<sentence.length;i++){
            System.out.println((i+1)+"\t\t"+vowel[i]+"\t\t"+word[i]);
        }
    }
    private String repeat(char what, int times){
        String output="";
        for( int i=1; i<=times;i++){
            output+=what;
        }
        return output;
    }
    public void plotHistogram(){
        int width=50;
        float scalingFactor=width/max;
        System.out.println("Sentence\tNo. of vowels / words");
        for(int i=0; i<sentence.length;i++){
            System.out.println((i+1)+"\t"+repeat('V', (int)(vowel[i]*scalingFactor)));
            System.out.println("\t"+repeat('W', (int)(word[i]*scalingFactor)));
        }
        System.out.println("Scale used 1:"+scalingFactor);
    }
}
public class ISC2008Q2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("INPUT:\t");
        String paragraph = in.nextLine();
        Text obj = new Text(paragraph);

        System.out.println("OUTPUT:");
        obj.showStats();
        obj.plotHistogram();
        in.close();
    }
}

All these solutions in one free book

Pragmatic Solutions to ISC Computer Science Practical Problems collects every program from 2004 to 2021 in a 202-page PDF.

Get the free PDF ₹0 ISC 2027 mock papers