C-ansi. programs . Elementary Code

Due to formatting error, the user has to replace the parenthesis with pointy brackets in the #INCLUDE header, when pasting the program sourcecode into the Editor IDE .
A sourcecode IDE may be Codelite.

Editors : 
C : http://www.codelite.org
Assembly for X86 DOS : EMU8086 (64bit CPU): http://www.emu8086.com
TDM GCC 32 : http://tdm-gcc.tdragon.net/download

Instruction Resources : 
X86 Assembly Language and C Fundamentals (intel cpu) : Book
Art of Assembly : http://www.plantation-productions.com/Webster/www.artofasm.com/DOS/HardCopy.html .  PDF
Assembly ARM : http://www.peter-cockerell.net/aalp/html/frames.html
Assembly AMD : http://developer.amd.com/resources/developer-guides-manuals/
Most important : Click Here !

Knock yourself out !

Considering the C language. I might think its a good idea to study all the instructions or functions in each respective header file. Then do examples on each function, to built a personal liberary. The C language is not much good without the accompanying 'header.h' includes.

C reference C89 , C95, C99 , C11 : http://en.cppreference.com/w/c/header
C tutorials : https://www.tutorialspoint.com/c_standard_library/index.htm

//Program 1
//------------

   # include (stdio.h)     // !! Replace the parenthesis (), with brackets < > !
   # include (string.h)    // !! Replace the parenthesis (), with brackets < > !
   //Strings. String lenght terminator.
   //KHO2016.no1. mingw (TDM-GCC-32) . c-ansi .
   int main ()
   {
   //declare
   char src_str[20],dst_str[10];

   //valuate
   printf ("Enter a sentance of 20 letters\n");
   gets (src_str);
   strcpy (dst_str,src_str);

   //calculate
   dst_str [10] ='\0'; // from the "www.stack overflow"
   printf ("%s",dst_str);
   printf ("\n");

   //terminate
   return 0;
   }

//Program 2
//------------

#include (stdio.h)
int main ()
//Convert Trigonometric Angles into Decimals and Radians.
//Radians are number of Radiuses that are wrapped around the circumference.
//Pi for half the circle, Radius is wrapped 3.14 times on 180 degrees. r2=d1 .
//Circumference = 2radius * Pi = Diameter * Pi = 2Pi * radius .
//KHO2016.no2. mingw (TDM-GCC-32) . c-ansi .
{
//Declare
float flp1, flp2, flp3, flp4, pi;
int int1;

//valuate
pi = 3.141592654;
int1 = 180;

//calculate
printf ("type a degree between 0 - 360 : ");
scanf ("%f",&flp1);
flp2=int1/flp1;  // 180 is divided by desired angle
flp3=pi/flp2;    // Pi is divided by the result of 180 / desired angle = Radians
flp4=cosf(flp3); // Result of Pi divided by Radians and fed into the Cosf Radian modulus
printf ("The Decimal value of Cosinus %.1f degrees = %.3f\n",flp1,flp4);
printf ("Angle typed in Radians = %f",flp3);

//Terminate
return 0;

//Program 3
//------------

   #include (stdio.h)     // !! Replace the parenthesis (), with brackets < > !
   #include (math.h)      // !! Replace the parenthesis (), with brackets < > !
   //1D_Array(float). Create array, then divide 2 numbers and display sum.
   //KHO2016.no3. mingw (TDM-GCC-32) . c-ansi .
   int main()
   {
   //Declare
   int i,int1;
   float flp1,flp2,flp3,array[30];

   //valuate
   printf ("The program request 4 numbers\n");
   printf ("1. Number of Rows or loops\n2. Value above the fraction line\n");
   printf ("3. Value beneath the fraction line\n4. Value of Powernumber\n\n");

   jump :
   printf ("Type in number of loops, 1-30 :");
   scanf ("%d",&int1);
   if (int1>31)
   {printf ("Number is too large\n");
   goto jump;}

   else if (int1<=30)
   printf ("Type the number Above the Fraction line (Dividend) ");
   scanf ("%f",&flp1);
   printf ("Type the number beneath the Fraction line (Divisor) ");
   scanf ("%f",&flp2);
   printf ("Value of Powernumber ");
   scanf ("%f",&flp3);

   //calculate
   for (i<0;i<=int1-1;i++)
   {array[i]=(float)(flp1/pow(flp2, (++flp3)-1)); // <------ interesting="" part="" span="">
   printf ("Row [%d]\t%.0f / %.0f^%.0f = %.6f\n",i+1,flp1,flp2,flp3-1,(float)array[i]);}

   //terminate
   return 0;
   }

//Program 4
//------------

   #include (stdio.h)     // !! Replace the parenthesis (), with brackets < > !
   int main ()
   //2D_Array. Create 2D array . Multiplication in each array slot.
   //KHO2016.no4. mingw (TDM-GCC-32) . c-ansi .
   {
   //Declare
   int a,b,c,d,sum;
   int k,l,num_array[50][50];

   //vaulate
   jump1 :
   printf ("Coordinat system Descartes\nBasically two FOR-loops.\n\n");
   printf ("Plot X no. of Columns, Horizontal axis. Then Y no. of Rows, Vertical axis\n:\n");
   scanf ("%d %d",&a,&b); //a = vertical , b=Horizontal.
   printf ("\nPlot two no. to be multipled\n:\n");
   scanf ("%d %d",&c,&d);

   //calculate
   for (l=0;l<b;l++)   // l counts lines or on Y axis .
   {for (k=0;k<a;k++) // k counts colums (on each line) .
   printf ("[Y%d] [X%d] (%d * %d = %d)\t: ",l,k,c++,d,c*(d));
   sum = (sum+(c*d++));
   printf ("sum %d",sum);
   printf ("\n");}

   printf ("\n");
   goto jump1;  

   //terminate
   return 0;
   }

//Program 5
//------------

   #include (stdio.h)     // !! Replace the parenthesis (), with brackets < > !
   int main ()
   //1D_Array. Load Element and Add Sumline .
   //KHO2016.no5. mingw (TDM-GCC-32) . c-ansi .
   {
   //declare
   int a,b,c,d,e,sum1=0;
   int array[50];
   int i,j,elm1,elm2;

   //valuate
   printf ("Plot a number of elements [1 - 20]: ");
   scanf ("%d",&a);
   printf ("Plot a value : ");
   scanf ("%d",&b);
   printf ("Plot an increment value : ");
   scanf ("%d",&c);

   //calculate
   {for (i<0;i<=a;i++)
   {array[i] =(b+(++c)); // set value for variable [i], inside the array subscript. the vairable [i] must have an INT, and an increment to function !
   sum1 = (sum1 + array[i]);
   printf ("Row [%.2d] : %.2d + %.2d = %d\n",i,b,c,array[i]);}
   printf ("\nSum total = %d\n",sum1);} 
   printf ("\nRow [%.2d] = %d\n",b,array[b]);
   printf ("Row [%.2d] = %d\n",a,array[a]);
   printf ("Select 2 Rows :\n");
   scanf ("%d%d",&elm1,&elm2);
   d=elm1;
   e=elm2;

   printf ("You selected Row [%.2d] = %d\n",d,array[d]);
   printf ("You selected Row [%.2d] = %d\n",e,array[e]);
   printf ("The sum of two selected Rows [%d]+[%d] : %d + %d = %d\n",d,e,array[d],array[e],array[d]+array[e]);
   //terminate
   return 0;
   }

//Program 6
//------------

   #include (stdio.h)     // !! Replace the parenthesis (), with brackets < > !int main ()
   //2D_Array.(float). Select Coordinate, and obtain slot Value .
   //KHO2016.no6. mingw (TDM-GCC-32) . c-ansi . 
   {
   //declare
   int aa,c,d,p,q;
   float j,ar_d[10][10];

   //valuate
   jump0:
   printf ("Select 1, 2, 3, 4 : ");
   scanf ("%d",&aa);
   if (aa==1&&2&&3)
   goto jump0;

   //calculate
   if (aa==4)
   {for (c=0;c<10;c++)
   for (d=0;d<10;d++)
   ar_d[c][d]=(float)d/c;

   for (c=0;c<10;c++)
   {for (d=0;d<10;d++)
   printf ("(Y%d X%d.%.1d/%.1d= %.2f) ",c,d,c,d,ar_d[c][d]);
   printf ("SumLine1(%d)/(%d) = %.2f\n",c,d,j);
   j=(float)d/(c+1);}

   printf ("\nSelect a Coordinate : first X then Y\n"); 
   scanf("%d%d",&p,&q);
   printf ("Coordinate X[%d],Y[%d] has this value : %f\n",p,q,ar_d[q][p]);}
   goto jump0;

   //terminate
   return 0;
   }

//Program 7
//------------
http://stackoverflow.com/questions/14950123/c-access-multi-dimensional-array/39217171#39217171
//------------

   #include (stdio.h)     // !! Replace the parenthesis (), with brackets < > !
   int main()
   //2D_Array. Multilist. Sumline and Total Sum .
   //Select each coordinate by pressing ENTER after each number .
   //KHO2016.no7. mingw (TDM-GCC-32) . c-ansi .
   {
   //declare, valuate
   int a,b,c=1,d=1,i,j,k,l,sum0=0;
   int ar_a[20][20];

   //calculate
   jump0:
   for (l=0;l<1;l++)                // vary the value l<1 -="" l="" span="">
   {printf ("M.M.M Shelf %d\n",l); // SHELF2
   for (k=0;k<1;k++)              // SHELF1
   {printf ("SumLine%d = %d\n",k,sum0);
   {for (i=1;i<6;i++)           // COLUMS .
   for (j=0;j<1;j++)           // LINES . per COLUM 
   {ar_a[i][j]=d*c++;         // put value into 2D_array indevidual result slot.
   sum0=sum0+(ar_a[i][j]);
   printf ("%d%d(%.2d*%.2d=%.3d)\t",i,j,d,c-1,ar_a[i][j]);}}}}

   printf ("TOTAL SUM = %d . Select 2 coordinates [1-5] enter, [0] enter: \n",sum0);
   scanf ("%d%d",&a,&b);
   printf ("You selected : %d and %d . Result value = %d\n",a,b,ar_a[a][b]);
   goto jump0;

   //terminate
   return 0;
   }
//Program 8
//------------

#include (stdio.h)     // !! Replace the parenthesis (), with brackets < > !
//Divide Integers and retrieve the Quotient and Reminder, for Hex to Dec conversion.
//KHO2016.no8. mingw (TDM-GCC-32) . c-ansi .
int main ()
{
//Declare
int int1,int2,int3;
float flp1,flp2;

jump0:
printf ("-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
printf ("Dividing Radix 10 Decimals\nSelect\n1 for Integers\n2 for Floating numbers\n");
scanf ("%d",&int3);
if (int3==1)
goto jump1;
else if (int3==2)
goto jump2;
else
printf ("Thou has neither typed a number 1 nor 2, kind Sir .\n\n");
goto jump0;

jump1:
//Valuate 1 INT
printf ("\nDivide two Integer (whole number like 123) and retrieve the Reminder.\n\n");
printf ("Type in an Integer number : ");
scanf ("%d",&int1);
printf ("\nType another Integer number : ");
scanf ("%d",&int2);

//Calculate 1 INT
printf ("\n-------------------------");
printf ("\n%d / %d = Quotient %d\n",int1, int2, int1/int2);
printf ("=========================\n");
printf ("\nQuotient = %d",int1/int2);
printf ("\nReminder = %d\n\n\n",int1%int2);
goto jump0;

jump2:
//Valuate 2 FLP
printf ("\nDivide two Floating Point Numbers");
printf ("\n\nType in a fractional number (like 1.2) use POINT . DOT to separate : ");
scanf ("%f",&flp1);
printf ("\nType another fractional number (like 3.4) use POINT . DOT to separate : ");
scanf ("%f",&flp2);

//Calculate 2 FLP
printf ("\n-----------------------------------------");
printf ("\nFLP1 %f / FLP2 %f = %f\n",flp1, flp2, flp1/flp2);
printf ("=========================================\n");
printf ("\nFloating point Quotient use the %%.2f Modulus Format Specifyer.\n");
printf ("Meaning the FLP is rounded upward into 2 digits to the right of\n");
printf ("the Decimal Point ( Radix Point ) %%3.f gives 3 fraction decimals.");
printf ("\nThere is no Reminder in the Floating point. Integers must be used.");
printf ("\n\nQuotient %%.1f= %.1f",flp1/flp2);
printf ("\n\nQuotient %%.2f= %.2f",flp1/flp2);
printf ("\n\nQuotient %%.3f= %.3f",flp1/flp2);
printf ("\n\nQuotient = %f\n\n",flp1/flp2);
goto jump0;

//Terminate
return 0;
}

//Program 9
//String array for characters, Int array for numbers. Printed in FOR loops. User can change all values . And display RAM locations of pointers . KHO . 03.01.2017

#include (stdio.h)     // replace parenthesis with pointy brackets
//#include  // GCC32-C . mingw . compile
#define lenght 20    // Orbit_L75.Apartment_9

int main(){

int i,j,k,select,select2,*ptr;
char str1 [lenght];              
char str2 [lenght];             
char *wordarray [2]={str1,str2}; // character array must have a * asterix  pointer . 
int numarray [2];

printf ("Type your forname : ");
gets (str1); // gets (wordarray[0]) // alternative syntax       
printf ("Type your last name : ");
gets (str2);            

printf ("Enter your telephone number : ");            
scanf ("%d",&numarray[0]);     // assign a value to numarray slot 0
//scanf ("%d",(numarray+0));  // alternative syntax

printf ("Enter your age : ");
scanf ("%d",&numarray[1]); // assign a value to numarray slot 1
printf ("\n\n");

jump1 : 
printf ("=========================\n");
for (i=1;i<5;i++)
{printf ("%d\t",i);}
printf ("\n");

for (j=0;j<2;j++)
{printf ("%s\t",wordarray[j]);}
//printf ("%s\t",*(wordarray+j));} //  alternative syntax
printf ("\n");

for (k=0;k<2;k++)
{printf ("%d\t",numarray[k]);}
printf ("Sum = %d\n",(numarray[0]+numarray[1]));     // add numarray slot 0 and slot 1.
//printf ("Sum = %d",*(numarray+0)+*(numarray+1));  // alternative syntax
printf ("=========================\n");

printf ("\n\nSelect\n1: Change Telephone \n2: Change Age \n3: Change First Name \n4: Change Last Name \n5: RAM location\n");
scanf ("%d",&select);

if (select == 1)
{printf ("New number : ");
scanf ("%d",&numarray[0]);
//scanf ("%d",(numarray+0));               // alternative syntax
printf ("\n");}

else if (select == 2)
{printf ("New age : ");
scanf ("%d",&numarray[1]);
printf ("\n");}

else if (select == 3)
{printf ("New First Name : ");
scanf ("%s",str1); //problems with the display using GETS on the second run.
printf ("\n");} 

else if (select == 4)
{printf ("New Last Name : ");
scanf ("%s",str2);
printf ("\n");}

else if (select == 5)
{ // select2
{printf ("\nRAM location of : \n\t1. Telephone number\n\t2. Age\n\t3. First Name.\n\t4. Last Name\n");}
scanf ("%d",&select2);

if (select2 == 1)
{ptr = &numarray[0];
printf ("\nTelephone number\nValue in Decimal\t: %d\nValue in Hexadecimal\t: %ph\nRAM location in decimal\t: %d\nRAM location in Hex\t: %ph\n\n\n",*ptr,*ptr,ptr,ptr);}

else if (select2 == 2)
{ptr = &numarray[1];
printf ("\nAge\nValue in Decimal\t: %d\nValue in Hexadecimal\t: %ph\nRAM location in decimal\t: %d\nRAM location in Hex\t: %ph\n\n\n",*ptr,*ptr,ptr,ptr);}

else if (select2 == 3)
{ptr = &wordarray[0];
printf ("\nFirst Name\nValue in Text\t: %s\nValue in Hexadecimal\t: %ph\nRAM location in decimal\t: %d\nRAM location in Hex\t: %ph\n\n\n",*ptr,*ptr,ptr,ptr);}

else if (select2 == 4)
{ptr = &wordarray[1];
printf ("\nLast Name\nValue in Text\t: %s\nValue in Hexadecimal\t: %ph\nRAM location in decimal\t: %d\nRAM location in Hex\t: %ph\n\n\n",*ptr,*ptr,ptr,ptr);}

else if (select2 <1 || select2 > 4)
{printf ("\nValue is out of range, Try again .\n\n");}
} // end IF select2

else if (select <1 || select > 5)
{printf ("\nValue is out of range, Try again .\n\n");}

goto jump1;

return 0;
} // end MAIN
//Program 10
String copy


#include (stdio.h)  // replace () with '< >' pointy brackets
#include (string.h) // replace () with '< >' pointy brackets

// KHO2017.no10. GCC-C . mingw .
// Copy character strings from one place to another.
// Then display the entered type in a descending order.
// Last entered text on top, first text entered on the bottom.
// To see the different functions required for textstring operation
// and numerical operations.
    
int main()
{
    char int1[2]; // If just one character element, like here 
    char int2[2]; // add one slotcount from int1[1] to int1[2]
    char int3[2]; // for containing the /0 string terminator .

printf ("Type a character : ");
gets (int1); 
strcpy (int2,int1);

printf ("1. %s\n",int1);
printf ("\nChange value : ");
gets (int1);

printf ("2. %s\n",int1);
printf ("1. %s\n",int2);

jump1:
strcpy (int3,int2); // 'int2' is copied to 'int3', before
strcpy (int2,int1); // 'int1' is overwriting 'int2'

printf ("\nChange value again : ");
gets (int1);

printf ("3. %s\n",int1);
printf ("2. %s\n",int2);
printf ("1. %s\n",int3);

goto jump1;
	return 0;
}

// Program 11
copy numerical values.

#include (stdio.h)  // replace the () with '< >' pointy brackets
#include (string.h) // replace the () with '< >' pointy brackets
#include (stdlib.h) // replace the () with '< >' pointy brackets

int main ()
{
    float int1 [1]; // number of adress bytes (slots) in array.
    float *int2= malloc (sizeof(float)*1);
    float *int3= malloc (sizeof(float)*1);
    
// KHO2017.no11. GCC-C . mingw .   
// Copying numbers, from one place to another.
// Using the 'stdlib.h' header with 'malloc' and 'sizeof' functions, for proper operation.
// The purpose of the program is to type a value, store that value on an array slot, fx slot1.
// Copy the value in slot1, to slot2, then change the value in slot1.
// Slot2 contains the origional value entered by the user, previously.
// Then to display the values entered in a descending order, the last typed number on top.

// Using slot2 = slot1, would change value in slot2 when the user changes value in slot1.
// This may be well and good. But I can not copy from one array location
// to another, inside the same array. That is copy from 'int1 [0]' to 'int1 [1]' .

printf ("Type a Number : ");
scanf ("%f",&int1[0]); // Initial value entered

printf ("1. %.1f\n",int1[0]); // Value in int1[0] is displayed
memcpy (int2,int1,(sizeof(float)*1)); // array 'int1' is copied in another array 'int2'.

printf ("\nChange value : ");
scanf ("%f",&int1[0]); // Value of int[0] is changed .

printf ("1. %.1f\n",int1[0]); // Changed value in 'int1[0]' is displayed, before changed in the memcopy
printf ("2. %.1f\n",int2[0]); // Copy of 'int1[0], now in int2[0] displayed on screen, then changed in the memcopy
printf("\n");

jump1: // program cylinder-roll.

// Values are shifted and copied down the ladder . 
memcpy (int3,int2,(sizeof(float)*1)); // value 'int2' is copied to 'int3'.
memcpy (int2,int1,(sizeof(float)*1)); // value 'int1' is copied to 'int2'.


printf ("\nChange value again : ");
scanf ("%f",&int1[0]);
printf ("1. %.1f\n",int1[0]);
printf ("2. %.1f\n",int2[0]);
printf ("3. %.1f\n",int3[0]);

goto jump1; // program cylinder-roll.

return 0;
}










Kommentarer

Populære innlegg