Algorithm Analysis
Read the input numbers and find their sum.
Algorithm Implementation
Declare the working array.
double m[100];
Read the input array.
scanf("%d",&n); for(i = 0; i < n; i++) scanf("%lf",&m[i]);
Find the sum of all numbers in the array.
s = 0; for(i = 0; i < n; i++) s = s + m[i];
Output the desired sum.
printf("%.1lf\n",s);
Algorithm Implementation – Loop
#include <stdio.h> int i, n; double s, val; int main(void) { scanf("%d",&n); s = 0; for(i = 0; i < n; i++) { scanf("%lf",&val); s = s + val; } printf("%.1lf\n",s); return 0; }
Algorithm Implementation – Vector
#include <cstdio> #include <vector> using namespace std; int i, n; double s; vector<double> m; int main(void) { scanf("%d", &n); // allocate memory for vector, resize it m.resize(n); // read array for (i = 0; i < n; i++) scanf("%lf", &m[i]); // find the sum of array elements s = 0; for (i = 0; i < n; i++) s = s + m[i]; // print the answer printf("%.1lf\n", s); return 0; }
Algorithm Implementation – Dynamic Memory Allocation, malloc
#include <stdio.h> #include <malloc.h> int i, n; double s, *m; int main(void) { scanf("%d", &n); m = (double *)malloc(n * sizeof(double)); for (i = 0; i < n; i++) // read array scanf("%lf", &m[i]); s = 0; // find the sum of array elements for (i = 0; i < n; i++) s = s + m[i]; free(m); printf("%.1lf\n", s); // print the answer return 0; }
Algorithm Implementation – Dynamic Memory Allocation, calloc
#include <stdio.h> #include <malloc.h> int i, n; double s, *m; int main(void) { scanf("%d", &n); m = (double *)calloc(n, sizeof(double)); for (i = 0; i < n; i++) // read array scanf("%lf", &m[i]); s = 0; // find the sum of array elements for (i = 0; i < n; i++) s = s + m[i]; free(m); printf("%.1lf\n", s); // print the answer return 0; }
Algorithm Implementation – Dynamic Memory Allocation, new
#include <stdio.h> int i, n; double s; double *m; int main(void) { scanf("%d",&n); // allocate memory for array m = new double[n]; // read array for(i = 0; i < n; i++) scanf("%lf",m+i); // m + i = &m[i] // find the sum of array elements s = 0; for(i = 0; i < n; i++) s = s + m[i]; // print the answer printf("%.1lf\n",s); // deallocate the array memory delete[] m; return 0; }
Algorithm Implementation – Using Files
When working with files in the e-olymp system, the following are used:
input.txt
– input file;output.txt
– output file;
When submitting solutions with files, select the option "Solution uses files for reading and writing".
#include <stdio.h> int i, n; double s, m[100]; int main(void) { freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); scanf("%d",&n); // read array for(i = 0; i < n; i++) scanf("%lf",&m[i]); // find the sum of array elements s = 0; for(i = 0; i < n; i++) s = s + m[i]; // print the answer printf("%.1lf\n",s); return 0; }
Algorithm Implementation - STL
We will use the accumulate
function.
#include <cstdio> #include <numeric> using namespace std; int i, n; double s, *m; int main(void) { scanf("%d",&n); m = new double[n]; for(i = 0; i < n; i++) scanf("%lf",&m[i]); s = accumulate(m,m+n,0.0); printf("%.1lf\n",s); return 0; }
Java Implementation
public class Main { public static void main(String[] args) { Scanner con = new Scanner(System.in); con.useLocale(Locale.US); int n = con.nextInt(); double sum = 0; for (int i = 0; i < n; i++) { double val = con.nextDouble(); sum += val; } System.out.printf("%.1f",sum); con.close(); } }
Java Implementation – Custom Class
import java.util.*; class Array { private double[] m; private int size; public Array(int n) { size = n; m = new double[n]; } public void set(int i, double value) { m[i] = value; } public double getSum() { double s = 0; for(int i = 0; i < size; i++) s = s + m[i]; return s; } } public class Main { public static void main(String[] args) { Scanner con = new Scanner(System.in); con.useLocale(Locale.US); int n = con.nextInt(); Array a = new Array(n); for (int i = 0; i < n; i++) a.set(i,con.nextDouble()); System.out.printf("%.1f",a.getSum()); con.close(); } }
Python Implementation
n = int(input()) m = list(map(float,input().split())) print(sum(m))