Share This

Comp116 Lab Exercise Series : Lab 2

This is the lab exercise solution of lab 2 of Comp116 labsheet. The code is written in a simple way. But, it could have been written in a better way to actually take the equations instead of values only.

Question #1

Write a C++ program using function (pass by reference) that calculates the values of x and y from the two linear equations.
ax + by = m
cx + dy = n

The solutions are given as
x = (md - bn)/(ad - cb)
y = (na - mc)/(ad - cb)

The function should take eight arguments and return nothing.


#include <iostream>

using namespace std;

void solvelinear(float &x, float &y, int &a, int &b, int &c, int &d, int &m, int &n)
{
 x = ((m * d) - (b * n))/(float)((a * d) - (c * b));
 y = ((n * a) - (m * c))/(float)((a * d) - (c * b));
}

int main()
{
 float x, y;
 int a, b, c, d, m, n;
 cout << "Enter values for first equation(a b m): ";
 cin >> a >> b >> m;
 cout << "Enter values for second equation(c d n): ";
 cin >> c >> d >> n;
 solvelinear(x, y, a, b, c, d, m, n);
 cout << "Value of X: " << x << "\nValue of Y: " << y << endl;
 return 0;
}
Continue Reading...

Random Programming Tips For Beginners

While the programming courses in our university and other universities in Nepal give good basic foundation for programming, they lack the chapters on approach to better programming practices. In this post, I'll list some better programming practices intended for beginner programmers.


Select the proper variable and functions name so that it would be easier for you as well as other programmers to read your code. Remember you will start to puzzle if you check back your own unmanaged code.

Follow a proper convention for naming and STICK to it. You can follow any variables naming convention but you should always stick to it. (Some basic variable naming conventions are:
int iNum, char strName[40]
int i_num, char str_name[40]
It does not matter which you follow. What matters is how consistent you follow the convention.

Think before you code and sketch the structure of your program. Flowcharts, algorithms, pseudo-codes, etc. can prove great work before coding.

Don't ignore compiler warnings as those warnings probably indicate that your executable might be buggy in runtime.

Make the habit of writing comments as it would make the program even more clearer for others and yourself as well. But do not write excessive and unnecessary comments. Don't write comments in the lines that are self-explanatory.

Indent your source code as proper indentation makes your code look neat and increases readability.

Multiply rather than dividing as multiplication is generally faster than division.

If you've got any useful tips, leave them as comment and we'll add your tips in the post.
Continue Reading...

Character to ASCII IN CHAR() Format

This is a small code snippet that can be used to get the ASCII equivalent of your characters in the char() format which can be useful while performing SQL Injection.

//ascii maker for sql injection
//change the string part in the source code as per your need
// this can be used to create char() string for sql injection and this will come along with the video
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    char str[1024];
    int len;
    int i;
    FILE *fp;
    fp = fopen("sqlinj.txt","a+");
    if (fp == NULL)
    {
           printf("Error opening file");
           exit(1);
    }
    else
    {
        printf("ASCII CHAR() MAKER FOR SQL INJECTION");
        printf("\n\t\tBy SAM207");
        printf("\nEmail: samar_acharya[at]hotmail.com");
        printf("\n\nEnter the string: ");
        gets(str);
        len = strlen(str);
        for (i = 0; i<len;i++)
        {
            fprintf(fp,"char(%d),",str[i]);
        }
        fprintf(fp,"\n\n");
        fclose(fp);
    }
    printf("File successfully written");
    getch();
}
Continue Reading...

Comp116 Lab Exercise Series : Lab 1

We've started to share the lab exercises of Object Oriented Programming course of this running semester. While we will share fully working code, we expect you to grab the idea from codes and try writing the code on your own. This way, it will help you learn the language.

To access the lab exercises, you can visit the official webpage by clicking here. In this episode, we will post the lab 1's solutions.
Question #1
Write a C++ program that reads three coefficients a, b and c for quadratic equation and finds whether the solutions are in real or imaginary. (ax2 + bx + c = 0 if b2-4ac >=0 then the solutions are real.)
/*
*Compiled using g++
*g++ -o 1_1 1_1.cpp
*/
#include <iostream>

using namespace std;

int main()
{
 int a, b, c, m;
 cout << "Enter the values of a, b and c: ";
 cin >> a >> b >> c;
 m = (b * b) - (4 * a * c);
 if (m >= 0)
 {
  cout << "Roots are real" << endl;
 }
 else
 {
  cout << "Roots are imaginary" << endl;
 }
 return 0;
}

Question #2
Write a C++ program that reads three coefficients a, b and c for quadratic equation and finds whether the solutions are in real or imaginary. (ax2 + bx + c = 0 if b2-4ac >=0 then the solutions are real.)
/*
*Compiled using g++
*g++ -o 1_2 1_2.cpp
*/
#include <iostream>

using namespace std;

//returns largest number in array arr[] of size n
int get_largest(int arr[], int n)
{
 int largest = arr[0]; //assume first element is largest, now we perform 'less than' test with it
 for (int i = 1; i <= n; i++)
 {
  if (largest < arr[i])
  {
   largest = arr[i];
  }
 }
 return largest;
}

int main()
{
 int a[10], largest;
 for (int i = 0; i < 10; i++)
 {
  //do while loop for asking positive number when -ve value is given
  do
  {
   cout << "Enter the " << i+1 << "th number: ";
   cin >> a[i];
  } while (a[i] < 0);
 }
 largest = get_largest(a, 10);
 cout << "The largest number is " << largest << endl;
 return 0;
}

Please report us if there's any bug in the codes though we've tested them thoroughly.
Continue Reading...

Hello World In Different Languages

As with the famous trend of writing hello world as the first program in every beginner's site, we've written hello world programs in various different languages. So enjoy.

C
#include <conio.h>
int main()
{
printf("Hello World\n");
getch();
}

C#
class Hello {
  static void Main() {
    System.Console.Write("Hello World");
  }
 }

C++
#include <iostream>

using namespace std;

int main()
 {
  cout << "Hello, World." << std::endl;
 }

JAVA
public class Hello {
   public static void main(String []args) {
   System.out.println("Hello World");
   }
 }

Javascript
document.writeln("Hello, World");

PERL
print "Hello World\n"

PHP
<?php echo "Hello World"; ?> 

Python
print "hello world"

Ruby
puts "Hello, world"

Shell Scripting
#!/bin/sh
 echo "Hello World"

You can leave your addition in the comment section.
Continue Reading...

Welcome To KU Code Corners

Hello everybody this blog has been started in order to assist those newcomers who find it hard to learn computer programming. This blog would feature various source codes, project ideas, programming tutorials, questions, and contests on regular basis. We will soon start posting number of articles in this blog. So don't forget to bookmark us. Continue Reading...

Followers