Array and Structure in C++
This document contains two examples of C++ code: one for an array and one for a structure.
Below each code example, you will find the corresponding screenshot of the output
generated using Dev-C++.
Example 1: Array in C++
#include <iostream>
using namespace std;
int main() {
// Declare and initialize an array of 5 integers
int numbers[5] = {10, 20, 30, 40, 50};
// Print all elements in the array
cout << "Array elements are: ";
for(int i = 0; i < 5; i++) {
cout << numbers[i] << " ";
}
return 0;
}
Output:
Screenshot of the output will be here.
Example 2: Structure in C++
#include <iostream>
#include <cstring>
using namespace std;
// Define a structure to store student data
struct Student {
char name[50];
int age;
float grade;
};
int main() {
// Declare a structure variable
Student student1;
// Assign values to structure members
strcpy(student1.name, "John");
student1.age = 20;
student1.grade = 85.5;
// Print the student data
cout << "Student Name: " << student1.name << endl;
cout << "Student Age: " << student1.age << endl;
cout << "Student Grade: " << student1.grade << endl;
return 0;
}
Output:
Screenshot of the output will be here.