Looking for a Tutor Near You?

Post Learning Requirement ยป
x
x

Direction

x

Ask a Question

x

Hire a Tutor

PPT On C++ - Structs And Arrays

Loading...

Published in: C / C++
2,792 Views

A study of these C++ ppts will help the student get an idea about the fundamental concepts in c++. Very useful for 11, 12th grade CBSE students who have taken c++ in computer science. This ppt talks about structs and arrays.

Prerna S / Sharjah

4 years of teaching experience

Qualification: B.E in computer Science

Teaches: GRE, SAT, ACT, Maths, MS Office, Web Development, HTML, Java, English, Hindi, Science, Chemistry, Computer Science, Mathematics, English Language

Contact this Tutor
  1. Day 4
  2. Structs ' There are many instances in programming where we need more than one variable in order to represent an object. For example, to represent yourself, you might want to store your name, your birthday, your height, your weight, or any other number of characteristics about yourself struct Employee short id; int age; double wage;
  3. ' These variables that are part of the struct are called members (or fields). Keep in mind that Employee is just a declaration -- even though we are telling the compiler that the struct will have member variables, no memory is allocated at this time. By convention, struct names start with a capital letter to distinguish them from variable names. ' In order to use the Employee struct, we simply declare a variable of type Employee: ' Employee joe; // struct Employee is capitalized, variable joe is not
  4. As with normal variables, defining a struct variable allocates memory for that variable. It is possible to define multiple variables of the same struct type: Employee joe; // create an Employee struct for Joe Employee frank; // create an Employee struct for Frank
  5. Accessing struct members Employee joe; // create an Employee struct for Joe joe.id = 14; // assign a value to member id within struct joe joe.age = 32; // assign a value to member age within struct joe joe.wage = 24.15; // assign a value to member wage within struct joe Employee frank; // create an Employee struct for Frank frank.id = 15; // assign a value to member id within struct frank frank.age = 28; // assign a value to member age within struct frank frank.wage = 18.27; // assign a value to member wage within struct frank
  6. Initializing structs struct Employee short id; int age; double wage; Employee joe = { 1, 32, 60000.0 }; // joe.id = 1, joe.age = 32, joe.wage = 60000.0 Employee frank = { 2, 28 }; // frank.id = 2, frank.age = 28, frank.wage = 0.0 (default initialization)
  7. Structs and functions struct Employee short id; int age; double wage; void printlnformation(EmpIoyee employee) std::cout
  8. int main() Employee joe = { 14, 32, 24.15 }; Employee frank = { 15, 28, 18.27 // Print Joe's information printlnformation(joe); std::cout
  9. ' You are running a website, and you are trying to keep track of how much money you make per day from advertising. Declare an advertising struct that keeps track of how many ads you've shown to readers, what percentage of ads were clicked on by users, and how much you earned on average from each ad that was clicked. Read in values for each of these fields from the user. Pass the advertising struct to a function that prints each of the values, and then calculates how much you made for that day (multiply all 3 fields together).
  10. array ' An array is an aggregate data type that lets us access many variables of the same type through a single identifier. // allocate 30 integer variables (each with a different name) int testScoreStudent1; int testScoreStudent2; int testScoreStudent3; int testScoreStudent30;
  11. The following array definition is essentially equivalent: int testScore[30]; // allocate 30 integer variables in a fixed array
  12. #include int main() int prime[5]; // hold the first 5 prime numbers prime[0] = 2; // The first element has index 0 prime[l] = 3; prime[2] = 5; prime[3] = 7; prime[4] = 11; // The last element has index 4 (array length-I) std::cout
  13. Arrays can also be made from structs. Consider the following example: struct Rectangle int length; int width; Rectangle rects[5]; // declare an array of 5 Rectangle
  14. Passing arrays to functions #include void passVaIue(int value) // value is a copy of the argument value = 99; // so changing it here won't change the value of the argument void passArray(int prime[5]) // prime is the actual array prime[O] = 11; // so changing it here will change the original argument! prime[l] = 7 prime[2] = 5; prime[3] = 3; prime[4] = 2;
  15. int main() int value = 1; std::cout
  16. ' before passValue: 1 ' after passVaIue: 1 ' before passArray: 2 3 5 7 11 ' after passArray: 11 75 3 2
  17. Arrays and loops ' Consider the case where we want to find the average test score of a class of students. const int numStudents = 5; int scores[numStudents] = { 84, 92, 76, 81, 56 int totalScore = scores[O] + scores[l] + scores[2] + scores[3] + scores[4]; double averageScore = static_cast(totalScore) / numStudents;
  18. this cuts down on the number of variables declared significantly, but totalScore still requires each array element be listed individually. And as above, changing the number of students means the totalScore formula needs to be manually adjusted. int scores[] = { 84, 92, 76, 81, 56 const int numStudents = 5; int totalScore = O; // use a loop to calculate totalScore for (int student = O; student < numStudents; ++student) totalScore += scores[student]; double averageScore = static_cast(totaIScore) / numStudents;
  19. ' Loops are typically used with arrays to do one of three things: ' 1) Calculate a value (e.g. average value, total value) ' 2) Search for a value (e.g. highest value, lowest value). ' 3) Reorganize the array (e.g. ascending order, descending order)
  20. Here's an example of using a loop to search an array in order to determine the best score in the class: int main() int scores[] = { 84, 92, 76, 81, 56 const int numStudents = 5; int maxScore = O; // keep track of our largest score for (int student = O; student < numStudents; ++student) if (scores[student] > maxScore) maxScore = scores[student]; std::cout
  21. Multidimensional Arrays ' The elements of an array can be of any data type, including arrays! An array of arrays is called a multidimensional array. int = { 1, 2, 3, 4, 5 // row O { 6, 7, 8, 9, 10 // row 1 { 11, 12, 13, 14, 15 } // row 2
  22. Accessing elements in a two-dimensional array for (int row = O; row < numRows; ++row) // step through the rows in the array for (int col = O; col < numCoIs; ++col) // step through each element in the row std::cout
  23. A two-dimensional array example int main() const int numRows = 10; const int numCols = 10; int product[numRows][numCols] // Calculate a multiplication table for (int row = 0; row < numRows; ++row) for (int col = O; col < numCols; ++col) product[row][col] = row * col;
  24. // Print the table for (int row = 1; row < numRows; ++row) for (int col = 1; col < numCoIs; ++col) std::cout