tell me a different program in which function,array,loops and structure are used? i present it on tuesday

Dani AI

Generated

As asked for a single program that uses functions, arrays, loops and structures, and following suggestion to use an array of structs (and noting that posted separate snippets), the example below ties everything together into a compact C++ gradebook. ’s point about doing original work is important: this is a starting template to type, run and explain rather than copy-paste.

#include <iostream>
#include <string>
#include <iomanip>
#include <limits>

const int MAX = 10;

struct Student {
    std::string name;
    int marks[3];
    double avg;
    char grade;
};

void inputStudents(Student arr[], int &n) {
    std::cout << "Number of students (max " << MAX << "): ";
    std::cin >> n;
    if (n > MAX) n = MAX;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    for (int i = 0; i < n; ++i) {
        std::cout << "Name: ";
        std::getline(std::cin, arr[i].name);
        for (int j = 0; j < 3; ++j) {
            std::cout << "Mark " << (j+1) << ": ";
            std::cin >> arr[i].marks[j];
        }
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
}

void computeGrades(Student arr[], int n) {
    for (int i = 0; i < n; ++i) {
        int sum = 0;
        for (int j = 0; j < 3; ++j) sum += arr[i].marks[j];
        arr[i].avg = sum / 3.0;
        if (arr[i].avg >= 90) arr[i].grade = 'A';
        else if (arr[i].avg >= 80) arr[i].grade = 'B';
        else if (arr[i].avg >= 70) arr[i].grade = 'C';
        else if (arr[i].avg >= 60) arr[i].grade = 'D';
        else arr[i].grade = 'F';
    }
}

void printReport(const Student arr[], int n) {
    std::cout << std::left << std::setw(20) << "Name"
              << std::setw(8) << "Avg" << "Grade\n";
    for (int i = 0; i < n; ++i) {
        std::cout << std::setw(20) << arr[i].name
                  << std::setw(8) << std::fixed << std::setprecision(1) << arr[i].avg
                  << arr[i].grade << '\n';
    }
}

int main() {
    Student list[MAX];
    int n = 0;
    inputStudents(list, n);
    computeGrades(list, n);
    printReport(list, n);
    return 0;
}

Troubleshooting notes: compile with a modern compiler (for example g++ -std=c++11 file.cpp -o prog). Remember to include <limits> when using cin.ignore with numeric_limits. Mixing cin >> and getline requires clearing the newline (shown above). For larger or unknown counts, replace the fixed MAX array with std::vector<Student>. Finally, present this as a demonstrated attempt in class and explain each function’s role—this addresses the homework/academic-honesty concern raised by while using the structure idea from .

Recommended Answers

All 6 Replies

You do the work. We help you fix it. Don't ask us to do your homework for you - that is cheating! :-(

program different from what?

How about a program containing a function that creates an array of structures and loops over it?

Unfortunately, it's not clear from your question what is the exact processing required.

Please can you specify what you want more clearly - and then show a REAL attempt at answering the question - it's clear to anyone who's done any programming teaching that what you've shown in your question is the answer to some other assignment.

Here I am giving some examples of basic programs that you asked for. Check them out.

// function example
#include <iostream>
using namespace std;

int addition (int a, int b)
{
  int r;
  r=a+b;
  return r;
}

int main ()
{
  int z;
  z = addition (5,3);
  cout << "The result is " << z;

//Array Example

#include <iostream>
using namespace std;

#include <iomanip>
using std::setw;

int main ()
{
   int n[ 10 ]; // n is an array of 10 integers

   // initialize elements of array n to 0          
   for ( int i = 0; i < 10; i++ )
   {
      n[ i ] = i + 100; // set element at location i to i + 100
   }
   cout << "Element" << setw( 13 ) << "Value" << endl;

   // output each array element's value                      
   for ( int j = 0; j < 10; j++ )
   {
      cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl;
   }

   return 0;
}

/*  For loop Example            */

#include<iostream>
#include<conio.h>

using namespace std;

int main()
{

     // Variable Declaration
     int a;

     // Get Input Value
     cout<<"Enter the Number :";
     cin>>a;

     //for Loop Block
     for (int counter = 1; counter <= a; counter++)
     {
         cout<<"Execute "<<counter<<" time"<<endl;
     }

     // Wait For Output Screen
     getch();
     return 0;
 }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.