Skip to content

MyUstaadG

Learn to Serve

  • Computer Science
    • Software
    • Computer Tips
    • Coding
      • Conditional Structures Exercises
      • Loops Exercises
      • Arrays Exercises
      • Python Exercises
    • Microsoft Office
      • MS Excel
      • MS Word
  • Videos
    • Funny Videos
    • Health and Fitness Videos
    • PUBG Videos
    • Technology Videos
  • News
  • Funny
  • Memes
  • Playwright Tutorials
  • Tutorials
  • Toggle search form

Write a program that initializes values in a one-dimensional array and Calculate the sum of an array using function.

Posted on October 17, 2021 By myustaadg.com
Spread the love

Write a program that initializes values in a one-dimensional array. It passes the array as argument to a function which computes the sum of the array. The function should return the calculated sum to the calling function.

So let’s Write Code:

Introduction to C++ Programming | Download and Install Dev C++ Compiler

Let’s build logic First:

Algorithm for code:

  • Step 1: Start
  • Step 2: Declare an array and variable i for loop.
  • Step 3: Initialize array.
  • Step 4: Defining function for sum int sum(int x[]).
  • Step 5: add s + x[ i ] until i< =4 and store in s.
  • Step 6: Return s.
  • Step 7: Calling the function int sum( int [] );
  • Step 8: Print Result.
  • Step 9: End Program

Code:

Write a program that initializes values in a one-dimensional array. It passes the array as an argument to a function that computes the sum of the array. The function should return the calculated sum to the calling function.

#include<iostream>
using namespace std;
main()
{
int arr[5] = {4,6,3,4,7}, i;
int sum(int []);
cout<<"Values of array\n";
for(i = 0; i<=4; i++)
cout<<arr[i]<<endl;
cout<<"\nSum of array ="<<sum(arr);
}
// definition of sum() function
int sum(int x[])
{
int s = 0;
for(int i = 0; i<=4; i++)
s = s + x[i];
return s;
}

Hope You will learn Something. Happy Coding.

Visit my YouTube Channel as Well๐Ÿ˜‡ https://www.youtube.com/watch?v=JuIHQ9cLSEw&list=PLbIhkHxfUIItTdcyCb34uRIrPJbXBndIl&index=4

Click on the below Links for more Programming Exercises:

Conditional Structure Exercises: ๐Ÿ‘‰https://myustaadg.com/category/programming-exercises/conditional-structures-exercises/

Array Exercises: ๐Ÿ‘‰https://myustaadg.com/category/programming-exercises/arrays-exercises/

Arrays Exercises, Programming Exercises Tags:array declaration in c++, Array Exercises, Array exercises in C++, Arrays, arrays codes, Arrays exercises, Arrays in C++, Arrays in C++ Programming with examples, Arrays in Programming, Arrays with example, C++ array: declare and initialize, C++ program to add two integer numbers using function, C++ program to find sum of array elements, C++ Program to Find Sum of N Numbers Using Function, Cpp program to add two numbers using function, Creating and Using Arrays, dimensional array. It passes the array as an argument to a function that computes the sum of the array. The function should return the calculated sum to the calling function., How can you declare and initialization of one dimensional array, How do you calculate sum in C++?, How do you do an addition function in C++?, How to find the sum of array elements using the function in C++, One dimensional Array in C++, Program in C and C++ to calculate sum of array, Program to find sum of elements in a given array, Program to print the sum of all the elements of an array, Sum Function - C++ Programming Question, sum of two variables in c++ Code Example, Write a program that initializes values in a one-dimensional array. It passes the array as an argument to a function that computes the sum of the array.

Post navigation

Previous Post: Write a program to enter 10 integers in a single-dimensional array and then print out the array in descending order.
Next Post: Write a program that initializes roll numbers and marks of different subjects in a two-dimensional array.

More Related Articles

Write a program that inputs 10 numbers into an array and finds out the second largest value from that array. Arrays Exercises
Write a program that initializes an array with the number of days of different months. It inputs the number of month and displays the number of days of the entered month. Arrays Exercises
Write a program that inputs ages of different persons in an array and counts the number of persons that have ages between 65 and 80. Arrays Exercises
A toy store is very successful in two of its products: clowns and dolls. Suppose, Each clown weighs 112 g and each doll 75 g. Write a program that reads the number of clowns and dolls sold in the last order and calculates the total weight of the package to be shipped | python Programming Programming Exercises
Write a program which input age from user and display on the screen if he/she is of legal age or not | Python Programming Programming Exercises
Write a program that input amount from user to invest, the annual interest and the number of years, and shows on the screen the capital obtained in the investment | Python Programming Programming Exercises

Copyright © 2022 MyUstaadG.

Powered by PressBook Blog WordPress theme