stackoverflow1453

Programming&&Games&&Life

Archive for October, 2013

Write a program that right shifts an integer variable 4 bits.

/*

Name:
Copyright: Deitel C How to Program
Author: StackOverflow1453
Date: 10/31/2013 2:00:33 PM
Description:

Left shifting an unsigned integer by 1 bit is equivalent to multiplying the value 2. Write function power2 that takes two
integer arguments number and pow and calculates (number * 2 to the pow)
Use the shift operator to calculate the result. Print the values as integers and as bits.

*/
#include <stdio.h>
void displayInBitFormat(int);
int i, value, pow;

int main(void){

	puts("Enter an integer");
	scanf("%d",&value);

	printf("\n%d's bit representation is: \n",value);
	displayInBitFormat(value);
	puts("");

	puts("Enter power of two");
	scanf("%d",&pow);

	printf("result is: %d\n", value=value<<pow);
	puts("In bit format:");
	displayInBitFormat(value);

	getch();

}

//bit representer of an int
void displayInBitFormat(int val){

	int mask=1<<31;

	for (i = 0; i < 32; i++)
	{
		//here inclusive OR checked
		if (val&mask)
		{
			printf("1");

		}
		else
		{
			printf("0");
		}

		if ((i+1)%8==0)
		{
			printf(" ");
		}

		//here val's bits are shifted to left one by one
		val=val<<1;
	}

}

Provide an enumeration example

/*

Name:
Copyright: Deitel C How to Program
Author: StackOverflow1453
Date: 10/30/2013 2:37:07 PM
Description: Provide an enumeration example

*/
#include <stdio.h>

int i;

int main(void){

	enum months{JAN=1,FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};
	enum months month;

	char *dizi[13]= {"", "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"};


	for (month = JAN; month <= DEC; month++)
	{
		printf("\n%2d %s", month, dizi[month]);
	}

	getch();

}

Make the binary representation program more portable

/*

Name:
Copyright: Deitel C How to Program
Author: StackOverflow1453
Date: 10/30/2013 11:28:03 AM
Description: Displaying an integer in bits and make it more portable!!!!

*/
#include <stdio.h>
#include <limits.h>   //CHAR_BIT is in limits.h

void displayInBıtFormat(int);
int i, value;

int main(void){

	puts("Enter an integer");
	scanf("%d",&value);

	printf("\n%d's bit representation is: ",value);
	displayInBıtFormat(value);

	getch();

}

void displayInBıtFormat(int val){

	int mask=1<<(CHAR_BIT*sizeof(int)-1);

	for (i = 0; i < (CHAR_BIT*sizeof(int)); i++)
	{
		//here incluseve OR checked
		if (val&mask)
		{
			printf("1");

		}
		else
		{
			printf("0");
		}

		if ((i+1)%8==0)
		{
			printf(" ");
		}

		//here val's bits are shifted to left one by one
		val=val<<1;
	}

}

Display an integer in bits using bitwise operators.

/*

Name:
Copyright: Deitel C How to Program
Author: StackOverflow1453
Date: 10/30/2013 11:28:03 AM
Description: Displaying an integer in bits.

*/
#include <stdio.h>
void displayInBıtFormat(int);
int i, value;

int main(void){

	puts("Enter an integer");
	scanf("%d",&value);

	printf("\n%d's bit representation is: ",value);
	displayInBıtFormat(value);

	getch();

}

void displayInBıtFormat(int val){

	int mask=1<<31;

	for (i = 0; i < 32; i++)
	{
		//here incluseve OR checked
		if (val&mask)
		{
			printf("1");

		}
		else
		{
			printf("0");
		}

		if ((i+1)%8==0)
		{
			printf(" ");
		}

		//here val's bits are shifted to left one by one
		val=val<<1;
	}

}

Write a code to demonstrate struct and pointer to structs

/*

Name:
Copyright: 
Author: StackOverflow1453
Date: 10/24/2013 3:17:48 PM
Description: Assigning structures into each other


*/
#include <stdio.h>

struct record{

	char *name;
	char *surname;
	int studentNo;
	int score;

};

typedef struct record kayit;

int main(void){

	kayit stu1,stu2,stu3;

	kayit  *ptrStu3;

	ptrStu3= &stu3;

	stu1.name="Dirty";
	stu1.surname="Sisi";
	stu1.studentNo=133;
	stu1.score=55;

	stu2.name="Idiot";
	stu2.surname="Sisi";
	stu2.studentNo=144;
	stu2.score=98;

	stu3.name="İmbecile";
	stu3.surname="Sisi";
	stu3.studentNo=1321;
	stu3.score=93;


	printf("Student 1's name is: %s %s and he got %d from exam.\n", stu1.name, stu1.surname, stu1.score);
	printf("Student 2's name is: %s %s and he got %d from exam.\n", stu2.name, stu2.surname, stu2.score);

	stu1=stu2;

	puts("after assigning stu2 to stu1:");
	printf("Student 1's name is: %s %s and he got %d from exam.\n", stu1.name, stu1.surname, stu1.score);
	printf("Student 2's name is: %s %s and he got %d from exam.\n", stu2.name, stu2.surname, stu2.score);

	printf("Student 3's name is: %s %s and he got %d from exam.\n", ptrStu3->name, ptrStu3->surname, ptrStu3->score);

	getch();
}

Write a program that uses union

/*

Name:
Copyright: Deitel C How to Program
Author: StackOverflow1453
Date: 10/24/2013 2:55:57 PM
Description: What is the use of union?

*/
#include <stdio.h>

union record{

	int x;
	int y;

};

int main(void){

	union record myunion;

	myunion.x=14;
	puts("Printing content of x: ");
	printf("%d\n",myunion.x);

	myunion.y=15;

	puts("Printing content of y: ");
	printf("%d\n",myunion.y);

	/*
	below code outputs 15 because
	This union will take up the space of a single int, 
	rather than 2 separate int values. If the user set the value of x,
	and then set the value of y, it would overwrite the value of x since 
	they are both sharing the same memory location.
	*/
	puts("Printing content of x: after assigning y: ");
	printf("%d\n",myunion.x); 

	getch();

}

Write code that shows movement of a ball in two dimensions

/*

Name:
Copyright: 
Author: StackOverflow1453
Date: 10/24/2013 2:31:49 PM
Description: Write code that shows movement of a ball in two dimensions


*/
#include <stdio.h>
#include <math.h>

struct movement{

	float x;
	float y;

};

float t;

int main(void){

	struct movement ball;
	for (t = 0.0; t < 10; t=t+0.1)
	{
		ball.x=10-9*cos(t);
		ball.y=5+2*sin(t);

		printf("\nx=%f y=%f t=%f", ball.x, ball.y, t);
	}

	getch();

}