Here is a collection of programs for C++

A compiler for C



Example Program
  1. // C# Example Program
  2. // Input: An integer, listlen, where listlen is less than
  3. // 100, followed by listlen-integer values.
  4. // Output: The number of input values that are greater
  5. // than the average of all input values.
  6.  
  7. using System;
  8.  
  9. public class Ch2example {
  10.  
  11. static void Main() {
  12. int[] intlist;
  13. int listlen,
  14. counter,
  15. sum = 0,
  16. average,
  17. result = 0;
  18. intList = new int[99];
  19.  
  20. listlen = Int32.Parse(Console.readLine());
  21.  
  22. if ((listlen > 0) && (listlen < 100)) {
  23. // Read input into an array and compute the sum
  24. for (counter = 0; counter < listlen; counter++) {
  25. intList[counter] = Int32.Parse(Console.readLine());
  26. sum += intList[counter];
  27. } //- end of for (counter ...
  28.  
  29. // Compute the average
  30. average = sum / listlen;
  31.  
  32. // Count the input values that are > average
  33. foreach (int num in intList)
  34. if (num > average) result++;
  35. // Print result
  36. Console.WriteLine(
  37. "Number of values > average is:" + result);
  38.  
  39. } //- end of if ((listlen ...
  40. else
  41. Console.WriteLine("Error--input list length is not legal");
  42. } //- end of method Main
  43. } //- end of class Ch2example


Readability Example

  • Purpose: Demonstrates the use of structure in C++.
  • Stores some personal data in a structure, then prints it out

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. // Defining a structure
  7. struct PersonalData
  8. {
  9. char *FirstName;
  10. char *LastName;
  11. char *Birthday; // in the format of 12/30/1978
  12. int PhoneNum;
  13. }; // don't forget the ending ";"
  14. // Declaring a variable of type PersonalData
  15. PersonalData PersonOne;
  16.  
  17. // Populate PersonOne with data
  18. PersonOne.FirstName = "John";
  19. PersonOne.LastName = "Doe";
  20. PersonOne.Birthday = "12/30/1978";
  21. PersonOne.PhoneNum = 5855555;
  22.  
  23. // Print the data out
  24. cout << "PersonOne's First name is: " << PersonOne.FirstName << endl;
  25. cout << "PersonOne's Last name is: " << PersonOne.LastName<< endl;
  26. cout << "PersonOne's Birthday is: " << PersonOne.Birthday<< endl;
  27. cout << "PersonOne's Phone number is: " << PersonOne.PhoneNum<< endl;
  28.  
  29. return 0;
  30. }