One-dimensional arrays


Example 1: Creating and printing elements of one - dimensional array.
#include "stdafx.h"
#include "iostream"
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
       float x[5];
       cout << "x[0] = " << x[0] << "\n";
       cout << "x[1] = " << x[1] << "\n";
       cout << "x[2] = " << x[2] << "\n";
       cout << "x[3] = " << x[3] << "\n";
       cout << "x[4] = " << x[4] << "\n";
       system("pause");
       return 0;
}




Example 2: Creating and printing one-dimensional array using for loop.
// Array000.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
       float x[5];
       for(int i = 0; i < 5; ++i){
             cout << "x["<< i <<"] = " << x[i] << "\n";
       }
       system("pause");
       return 0;
}




Example 3: Creating your own array
// Array001.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream"
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
       int a[5] = {10, 20, 3, 4, 5}; // ERROR: PREVIĆ E INICIJALIZATORA
       for(int i = 0; i < 5; ++i){
             cout << " a[" << i <<"] = "<< a[i] << "\n";
       }
       system("pause");
       return 0;
}





Example4: Create and manipulate elements of one-dimensional arrays using arithmetic operators.
// Array002.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
       int x[5];
       cout << "x[0]=" << "\n";
       cin >> x[0];
       cout << "x[1]=" << "\n";
       cin >> x[1];
       cout << "x[2]=" << "\n";
       cin >> x[2];
       cout << "x[3]=" << "\n";
       cin >> x[3];
       cout << "x[4]=" << "\n";
       cin >> x[4];
       x[2] = x[0] + x[4] - 2* x[1];
       cout << "The result of x[2] = x[0] + x[4] - 2* x[1] is:" << x[2] << "\n";
       system("pause");
       return 0;
}




Share on Google Plus