0% found this document useful (0 votes)
34 views

C++ Assignment 1 Week 1

This C++ program defines a template function to calculate the sum of elements in a container. It initializes a vector with integers from 0 to 39, calls the sum function to calculate the total, and prints out the result. The sum function iterates through the container, adds each element to a running sum, and returns the final sum.

Uploaded by

Ganesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

C++ Assignment 1 Week 1

This C++ program defines a template function to calculate the sum of elements in a container. It initializes a vector with integers from 0 to 39, calls the sum function to calculate the total, and prints out the result. The sum function iterates through the container, adds each element to a running sum, and returns the final sum.

Uploaded by

Ganesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include<iostream>

#include<vector>

using namespace std;

const int N=40;

template<typename T>

int sum(T val)

int s = 0;

for(int i = 0; i < val.size(); ++i)

s +=val[i];

return s;

int main()

vector<int>vec;

for (int i=0;i<N;++i)

vec.push_back(i);

cout<<"sum is "<<sum(vec)<< '\n';

return 0;

You might also like