Personal tools
Document Actions

lesson2a.cpp

by aullmj — last modified 2009-05-20 11:54

Click here to get the file

Size 1.5 kB - File type text/x-c++src

File contents

//companion code for lecture on C++ syntax, conditionals and loops
//Mark Aull, 2008

/*points to mention: comments, whitespace doesn't do anything, for readability only
start lecture math ops, ++, --, +-* /=
printf, sprintf, strcat
conditional, loop (different from MATLAB)*/

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;

void main(void)
{//main function
	int ii=23;
	double p=3.1415;
	char string[80]="hello";
	printf("%s,%10d,%11.7f\n",string,ii,p);//displays __

	ii++;//increment to __
	p*=2;//p=__
	strcat(string," world");//concatenate
	printf("%s,%5d,%10.7f\n",string,ii,p);// displays __

	if (ii>23)//displays __
	{
		printf("ii >23\n\ttestf\b\b\bx\n");//note that only 's' is overwritten with 'x'
	}
	else if(ii==24)
		printf("ii=24\n");
	else
		printf("ii<=23\n");

	ii/=4;//decrement to __
	p-=ii;//p=__
	sprintf(string,"%s,%10d,%11.7f\n",string,ii,p);
	printf("%s",string);//displays __

	if (ii==6&&p==0)//displays __
	{
		printf("ii is 6 and p is 0\n");
	}
	else if(ii!=6)
		printf("ii is not 6\n");
	else
		printf("p is not 0\n");
	
	for (int jj=0;jj<2*ii;jj++)//scope of jj varies by compiler
	{
		printf("jj=%d\n",jj);
		if (jj<6)
			jj++;
	}

	p=5/2;// data type rules
	printf("p = %f\n",p);

	for (ii=4,jj=1;ii>1;jj*=ii,ii--);//null for loop
	printf("f(4)=%d\n",jj);//diaplays __

	for (ii=6,jj=1;ii>1;jj*=ii,ii--);
	printf("f(6)=%d\n",jj);//f(x)= __
}// end main
Related content