0% found this document useful (0 votes)
354 views16 pages

CS Students' Programming Guide

The document discusses three topics related to mathematical foundations of computer science: 1. Implementing a program in Java to simulate random movement of balls and graph the results as a curve. 2. Designing a program to simulate voting before and after a candidate's speech, analyzing the results using the WEKA tool. 3. Writing a program that takes the number of rooms and location of a house as input and outputs the price and classification (cheap, affordable, costly, very costly).

Uploaded by

happy happy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
354 views16 pages

CS Students' Programming Guide

The document discusses three topics related to mathematical foundations of computer science: 1. Implementing a program in Java to simulate random movement of balls and graph the results as a curve. 2. Designing a program to simulate voting before and after a candidate's speech, analyzing the results using the WEKA tool. 3. Writing a program that takes the number of rooms and location of a house as input and outputs the price and classification (cheap, affordable, costly, very costly).

Uploaded by

happy happy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Mathematical Foundation of Computer Science

1. Analyze the concept of randomization. Implement a program in a language that


supports graphics to Push the balls from left and right allowing random movement
and then let them fall in rectangle bins. Show graphically how they form curve.

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class Game extends JPanel
{
//changing these values will change the size of the game, whi
le still remaining functional
//within the size limit specified.
static int windowWidth = 1300;
static int windowHeight = 800;
int circleWidth = 25;
int circleHeight = 25;
int circleXLocation = 0;
int circleYLocation = 0;
boolean numberCreated = false;
static boolean gameRunning = false;
String direction="";
public void start() {
gameRunning = true;
}
public void generateRandomNumber() {
Random rand = new Random();
circleXLocation = rand.nextInt(windowWidth-circleWidth);
// System.out.println(circleXLocation);
numberCreated = true;
if(circleXLocation<windowWidth/2)
direction="left";
else
direction="right";
}
public void update() {
//calls the generateRandomNumber() method which gives the square
a random x value inside the screen
if (!numberCreated) {
generateRandomNumber();

1
Mathematical Foundation of Computer Science

}
if(circleYLocation==windowHeight){
//System.out.println(circleYLocation);
numberCreated=false;
circleYLocation = 0;
}
else{
if(direction.equals("left")){
if(circleXLocation == 0)
{
direction="right";
}
else
{
circleXLocation--;
circleYLocation++;
System.out.println("this is X value "+ circle
XLocation);
System.out.println("this is Y value "+ circle
YLocation);
}
}
else
{
if(direction.equals("right")){
if((circleXLocation + circleWidth) == windowWidt
h)
{
direction="left";
}
else
{
circleXLocation++;
circleYLocation++;
System.out.println("this is X value "+ circle
XLocation);
System.out.println("this is Y value "+ circle
YLocation);
}
}
}
}

2
Mathematical Foundation of Computer Science

}
public void paint(Graphics g) {
g.setColor(Color.black);
g.fillRect(0, 0, windowWidth, windowHeight);
g.setColor(Color.BLUE);
g.fillOval(circleXLocation,circleYLocation,circleWidth, circ
leHeight);
}
public static void main(String args[]) throws InterruptedExcepti
on{
Game ball=new Game();
JFrame frame = new JFrame();
frame.add(ball);
frame.setVisible(true);
frame.setSize(windowWidth, windowHeight);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_C
LOSE);
frame.setTitle("Random ball movement");
frame.setResizable(false);
frame.setLocationRelativeTo(null);
ball.start();
//updates square position, repaints square, and slows down u
pdate and paint speed.
while (gameRunning) {
ball.update();
ball.repaint();
Thread.sleep(1);
// gameRunning=false;
}
}
}

3
Mathematical Foundation of Computer Science

Output:

4
Mathematical Foundation of Computer Science

2. Consider that there are two parties’ party1 and party2 contesting for elections.
Consider candidate from either party1 or party2. Consider voting population and ask
them about their likelihood to vote for the candidate from party1. Now ask the
population again to show their likelihood for candidate after candidate gives speech.
Now again rate the candidate through voting population. Implement a program to do
this. Use WEKA tool to simulate this.

#include<stdio.h>
int main()
{
int voters,vote[10],count,n = 0,freq[10],i, j;
//char total_no_yes;
float percentage;
printf("..........Welcome for voting..........");
printf("\n Enter the total no of voters: ");
scanf("%d", &voters);
printf("\n ..........Before Speach vot for A .......... ");
for(i = 0; i<voters; ++i)
{
printf("\n enter vote for A (Voters: %d): ",i+1);
scanf("%d", &vote[i]);
freq[i] = -1;
if (vote[i] == 1)
{
n++;
}
}
for(i=0; i<voters; i++)
{
count = 1;
for(j=i+1; j<voters; j++)
{
/* If duplicate element is found */
if(vote[i] == vote[j])
{
count++;

/* Make sure not to count frequency of same element


again */
freq[j] = 0;

5
Mathematical Foundation of Computer Science

}
}
/* If frequency of current element is not counted */
if(freq[i] != 0)
{
freq[i] = count;
}
}
printf("\nFrequency of all elements of array : \n");
for(i=0; i<voters; i++)
{
if(freq[i] != 0)
{
printf("Total no of %d is comes %d times\n", vote[i], fr
eq[i]);
}
}

percentage = (float)n / voters * 100.0;


printf("\nPercentage = %.2f%%", percentage);

printf("\n ..........After Speach vot for A .......... ");


for(i = 0; i<voters; ++i)
{
printf("\n enter vote for A (Voters: %d): ",i+1);
scanf("%d", &vote[i]);
freq[i] = -1;
if (vote[i] == 1)
{
n++;
}
}
for(i=0; i<voters; i++)
{
count = 1;
for(j=i+1; j<voters; j++)
{
/* If duplicate element is found */
if(vote[i] == vote[j])
{

6
Mathematical Foundation of Computer Science

count++;

/* Make sure not to count frequency of same element


again */
freq[j] = 0;

}
}
/* If frequency of current element is not counted */
if(freq[i] != 0)
{
freq[i] = count;
}
}
printf("\nFrequency of all elements of array : ");
for(i=0; i<voters; i++)
{
if(freq[i] != 0)
{
printf("Total no of %d is comes %d times\n", vote[i], fr
eq[i]);
}
}

percentage = (float)n / voters * 100.0;


printf("\nPercentage = %.2f%%", percentage);

return 0;

7
Mathematical Foundation of Computer Science

Output:

8
Mathematical Foundation of Computer Science

Now Done Program in WEKA:

9
Mathematical Foundation of Computer Science

3. Write a program that takes two inputs- size of the house (no of rooms) and location
of the house and accordingly give price of the house. Classify the house as very costly,
costly, affordable, and cheap.

#include<stdio.h>
int main()
{
char remark;
int ROOMnum, location_num, room_Prize;
printf(" how many rooms did you want:\n");
scanf("%d", &ROOMnum);
if (ROOMnum > 4)
{
printf("enter bellow of 4\n");
}
else
{
printf("add your location here :\n");
printf("1. Adajan\n");
printf("2. Vesu\n");
printf("3. Athvalince\n");
scanf("%d",&location_num);
switch(location_num){
case 1:
printf("\nenter your Prize in adajan\n");
scanf("%d", &room_Prize);
if (room_Prize >= 150000)
{
printf("it is cheap\n");
}
else if (room_Prize >= 250000)
{
printf("it is Afordable\n");
}
else if (room_Prize >= 350000)
{
printf("it is coslty\n");
}
else
{
printf("it is very coslty\n");
}

10
Mathematical Foundation of Computer Science

break;
case 2:
printf("\nenter your Prize in Vesu\n");
scanf("%d",&room_Prize);
if (room_Prize >= 150000)
{
printf("it is cheap\n");
}
else if (room_Prize >= 250000)
{
printf("it is Afordable\n");
}
else if (room_Prize >= 350000)
{
printf("it is coslty\n");
}
else
{
printf("it is very coslty\n");
}
break;
default:
printf("\nenter proper value\n");
}
}
printf("\nYour Detail:\n");
printf("Room No : Loaction : Prize : Remark \n ");
printf(" %d : %d : %d : %c \n", ROOMnum, location_
num, room_Prize, remark);
}

11
Mathematical Foundation of Computer Science

Output:

12
Mathematical Foundation of Computer Science

4. Consider website of your institute. Represent the link structure by directed graph.
Apply and implement algorithm to traverse the graph and to reach a faculty's web
page in your department.

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

/* A binary tree node has data, pointer to left child


and a pointer to right child */
struct node
{
int data;
struct node* left;
struct node* right;
};

/* Prototypes for funtions needed in printPaths() */


void printPathsRecur(struct node* node, int path[], int pathLen);
void printArray(int ints[], int len);

/*Given a binary tree, print out all of its root-to-leaf


paths, one per line. Uses a recursive helper to do the work.*/
void printPaths(struct node* node)
{
int path[1000];
printPathsRecur(node, path, 0);
}

/* Recursive helper function -


- given a node, and an array containing
the path from the root node up to but not including this node,
print out all the root-leaf paths.*/
void printPathsRecur(struct node* node, int path[], int pathLen)
{
if (node==NULL)
return;

/* append this node to the path array */


path[pathLen] = node->data;
pathLen++;

13
Mathematical Foundation of Computer Science

/* it's a leaf, so print the path that led to here */


if (node->left==NULL && node->right==NULL)
{
printArray(path, pathLen);
}
else
{
/* otherwise try both subtrees */
printPathsRecur(node->left, path, pathLen);
printPathsRecur(node->right, path, pathLen);
}
}

/* UTILITY FUNCTIONS */
/* Utility that prints out an array on a line. */
void printArray(int ints[], int len)
{
int i;
for (i=0; i<len; i++)
{

printf("%d ", ints[i]);


printf("\t");
}
printf("\n");
}

/* utility that allocates a new node with the


given data and NULL left and right pointers. */
struct node* newnode(int data)
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;

return(node);
}

14
Mathematical Foundation of Computer Science

/* Driver program to test above functions*/


int main()
{

/* Constructed binary tree is


10-> Home
/ \
8-> Course 2-> 2K18
/ \
3->engin 5->Delet
/
9->Faculty
*/

struct node *root = newnode(10);


printf(" Home ->");
root->left = newnode(8);
printf(" Course ->");
//root->right = newnode(2);
root->left->left = newnode(3);
printf(" Enginnering ->");
root->left->left->left = newnode(9);
printf(" Faculty \n");

printPaths(root);

return 0;
}

15
Mathematical Foundation of Computer Science

Output:

16

You might also like