An algorithm to insert n elements in an array.
##An algorithm to insert n elemnts in an array.
Flowchart:
Now we will implement this flowchart in a program.
Source Code In C++:
#include <iostream>
using namespace std;
int main(int argc, char const *argv[]) {
int i,x,n,a[100];
std::cout << "How many elements:" ;
std::cin >> n;
std::cout << "Take all the elements to be inserted: " << '\n';
for (size_t i = 0; i <n; i++) {
std::cin >> a[i];
}
std::cout << "Print the elements:" << '\n';
for (size_t i = 0; i <n; i++) {
std::cout << a[i] << '\n';
}
return 0;
}
Explanation:
Here we took five elements to be inserted in the array.Then after insertion we printed it.
Flowchart:
Now we will implement this flowchart in a program.
Source Code In C++:
#include <iostream>
using namespace std;
int main(int argc, char const *argv[]) {
int i,x,n,a[100];
std::cout << "How many elements:" ;
std::cin >> n;
std::cout << "Take all the elements to be inserted: " << '\n';
for (size_t i = 0; i <n; i++) {
std::cin >> a[i];
}
std::cout << "Print the elements:" << '\n';
for (size_t i = 0; i <n; i++) {
std::cout << a[i] << '\n';
}
return 0;
}
Explanation:
Here we took five elements to be inserted in the array.Then after insertion we printed it.
No comments