Ok, let's look again at the code in C++ and understand what's going on in terms of algorithms and what not:
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <conio.h>
#include <math.h>
#define MAX 100
int main(){
int n, k, s = 0, cnt = 1, t[MAX] = {0};
cout<<"Submit an entry n: ";
cin>>n;
if((n < 5)||((n-5)%3)){
cout<<endl<<"Entry n does not exist in the series.";
cout<<endl<<"The sum S is 0."<<endl;
cout<<endl<<"There are no multiples of 4 between the numbers of n.";
}
else{
for(int i = 5; i <= n; i = i + 3){
(i%2) ? s = s + i : s = s - i; // Calculate sum S
if(!(i%4)){ // Determining multiples of 4 between the numbers of n
t[cnt] = i;
cnt++;
}
}
cout<<"The sum S is "<<s<<endl<<endl;
cout<<endl<<"The multiples of 4 between the numbers of n are:"<<endl;
for (int i = 0; i <= cnt-1; i++)
(i==cnt-1)? cout<<t[i]<<endl<<endl : cout<<t[i]<<" - ";
cout<<"Specify the entry position value k: ";
cin>>k;
cout<<endl<<"The kth entry in the series is: "<<pow(-1,k+1)*(2+3*k)<<endl;
}
getch();
return 0;
}
The first part of the code:
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <conio.h>
#include <math.h>
corresponds to importing the libraries where the functions and commands, that I want to use later on in the code, exist.
The part:
#define MAX 100
defines a global constant called "MAX" as 100, which I intend to use as the size of the array "t" to be defined later on. This is so if I want to alter the maximum size of "t" later on, I only have to change the value of "MAX" and not track my entire code to spot where "t" is defined. Here, the scope of the program is small, so it doesn't really matter whether I define "MAX" as a global or local constant. But, it's always good practice for later on when the code scope gets larger.
The core of the code in C++ is always written in "main", the main function:
The main function is the point by where all C++ programs start their execution, independently of its location within the source code. It does not matter whether there are other functions with other names defined before or after it - the instructions contained within this function's definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that all C++ programs have a main function.
The variables are:
int n, k, s = 0, cnt = 1, t[MAX] = {0};
where n is the entered variable by the user to be determined if it belongs to the series S (in short, it's the same n in your problem description), k is the entered variable by the user to determine the kth entry of the series S (same k as problem description), s is the series sum (initialized to zero), cnt is a counter that I used to keep track of how many entries are in the array t (in other words, how many multiples of 4 exist between the numbers of n, also initialized to zero), and finally the array t whose size is MAX (ie 100) so it can hold up to MAX entries ?(all entries are initialized to zero).
In C++, cout is used to display my data on the command prompt (similar to "ecrire" in yours), and cin is used to read data (similar to "lire"), hence the two lines:
cout<<"Submit an entry n: ";
cin>>n;
The code corresponding to determining whether the entered number by the user "n" exists in the series is:
if((n < 5)||((n-5)%3)){
cout<<endl<<"Entry n does not exist in the series.";
cout<<endl<<"The sum S is 0."<<endl;
cout<<endl<<"There are no multiples of 4 between the numbers of n.";
}
where "%" is the modulus sign in C++, "||" is the logic OR (like "ou") and "endl" is used to move to a new line.
Notice how everything related to the decisions to be made if entry "n" belongs to the series is placed in an "else" statement ("if" is like "si" and "else" is like "si non").
Now let's look at this code:
for(int i = 5; i <= n; i = i + 3){
(i%2) ? s = s + i : s = s - i; // Calculate sum S
if(!(i%4)){ // Determining multiples of 4 between the numbers of n
t[cnt] = i;
cnt++;
}
}
The "for" statement is identical to "pour i allant de 5 a n pas 3 faire". The statement:
(i%2) ? s = s + i : s = s - i;
was unfair of me to use; it translates to:
if(i%2)
s = s + i;
else
s = s - i;
Meaning, I check if my current number i is even, and add to to sum s with assigning it the appropriate sign (just like you did in your code supra). The code:
if(!(i%4)){ // Determining multiples of 4 between the numbers of n
t[cnt] = i;
cnt++;
}
checks if current number i is a multiple of 4 (if so, i%4 = 0; that's why when I used the statement "i%4" to be the logical decision maker for the if statement, I negated it with "!" which is logical NOT in C++ => the if statement is executed only if i is a multiple of 4). If i is a multiple of 4, this number is stored in the array t and has a position cnt in the array ie i would be the (cnt)th multiple of 4.
The sum S and the multiples of 4 are displayed with the code:
cout<<"The sum S is "<<s<<endl<<endl;
cout<<endl<<"The multiples of 4 between the numbers of n are:"<<endl;
for (int i = 0; i <= cnt-1; i++)
(i==cnt-1)? cout<<t[i]<<endl<<endl : cout<<t[i]<<" - ";
The code used to ask for the user to enter k, store k, and display the kth entry of the series S is:
cout<<"Specify the entry position value k: ";
cin>>k;
cout<<endl<<"The kth entry in the series is: "<<pow(-1,k+1)*(2+3*k)<<endl;
where "pow" stands for power, and as I stated in previous posts the kth entry is (-1)^(k+1)*(2+3k).
The lines:
getch();
return 0;
take a character and returns the value 0 to the main function since it's of type int. This is just standard C++ coding and has nothing to do with the algorithms/computations in the program.
Hope this makes my code a little bit clearer :)