In C++
#include<iostream> using namespace std; //Implement the function below. bool is_fibonacci_array(int*,int); //Param 1: pointer to the beginning of an array of integers //Param 2: size of that array //Returns: true, is every element in the input array is a Fibonacci number //(the order of the numbers in the array need not be ordered //as per the Fibonacci sequence) //false, otherwise //A Fibonacci sequence is generated as follows. //The first two numbers in the sequence are 0 and 1. //The following numbers in the sequence are calculated by adding //the previous two numbers in the sequence. Thus the third number in the //sequence would be 1 (i.e., 0+1), the fourth being 2 (i.e., 1+1), the //fifth being 3 (i.e., 1+2), and so on, giving the following sequence: //0, 1, 1, 2, 3, 5, 8, 13, 21, 34, etc. int main(){ //this is a Fibonacci array int a[5]={317811,28657,75025,3,8}; //this is NOT a Fibonacci array int b[3]={0,1,4}; int flag=is_fibonacci_array(a,5); //should print "Is a Fibonacci array." if (flag) cout << "Is a Fibonacci array.\n"; else cout << "Is not a Fibonacci array.\n"; flag=is_fibonacci_array(b,3); //should print "Is not a Fibonacci array." if (flag) cout << "Is a Fibonacci array.\n"; else cout << "Is not a Fibonacci array.\n"; }
Program :
#include <iostream>
#include <math.h>
using namespace std;
// Its an utility function which returns true if x is a perfect square
bool isPerfectSquare(int x)
{
int s = sqrt(x);
return (s*s == x);
}
// Its an utility function which returns true if n is a Fibonacci Number, else false
bool is_fibonacci_array(int inputArray[] , int size)
{
bool flag;
// n is Fibonacci if one of (5*n*n + 4) or (5*n*n - 4) or both is a perferct square
for(int j=0; j < size; j++){
int n = inputArray[j];
flag = isPerfectSquare(5*n*n + 4) ||
isPerfectSquare(5*n*n - 4);
if(!flag){
break;
}
}
return flag;
}
// main function where program starts
int main()
{
int size;
// asking user to enter the size of array
cout << "Enter the size of array" << endl;
// storing entered size in size variable of int type
cin >> size;
int inputArray[size];
// asking user to enter array values and storing them in inputArray of type int
for(int i=0; i<size; i++)
{
cout << "Value of inputArray[" << i << "] : " << endl;
cin >> inputArray[i];
}
// calling is_fibonacci_array and storing result in output of boolean type
bool output = is_fibonacci_array(inputArray, size);
// printing whether entered array is fibonacci or not.
if(output)
cout << "Is a Fibonacci array. \n" << endl;
else
cout << "Is not a Fibonacci array.\n";
return 0;
}
sample input and output :
Program snapshot :
I have divided the whole program into 2 parts since its very big in no. of lines.
In C++ #include<iostream> using namespace std; //Implement the function below. bool is_fibonacci_array(int*,int); //Param 1: pointer to...
c++ #include <iostream> #include <string> using namespace std; /* Write a function checkPrime such that input: an int output: boolean function: Return true if the number is a prime number and return false otherwise */ //TO DO ************************************** /* Write a function checkPrime such that input: an array of int and size of array output: nothing function: Display the prime numbers of the array */ //TO DO ************************************** /* Write a function SumofPrimes such that input: an int output: nothing...
#include#includeusing namespace std;// Implement printArray here// Implement deleteSmallest here// DO NOT CHANGE MAIN FUNCTION BELOWint main() {int myarray[100];cout << "Enter number of integers : ";int n;cin >> n;cout << "Enter " << n << " integers" << endl;for (int i = 0; i < n; i++)cin >> myarray[i];cout << "Contents of array : ";printArray(myarray, n);deleteSmallest(myarray, n);cout << "Contents of array after deleteSmallest: ";printArray(myarray, n-1);}
#include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...
// PLACE YOUR NAME HERE #include <iostream> using namespace std; float findAverage (int [], int); // finds average of all //grades int findHighest (int [], int); // finds highest of all //grades int findFirstFail( int[]); int main() { int grades[100]; // the array holding 100 grades. int numberOfGrades; // the number of grades read. int pos; // index to the array. float avgOfGrades; // contains the average of the grades. int highestGrade; // contains the highest grade. int inderOfFail; //...
How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0, b = 1, c; if (n <= 1) return n; for (int i = 2; i <= n; i++) { c = a + b; a = b; b = c; } return b; } int fibonacciRecursive(int n) { if (n <= 1) { return n; } return fibonacciRecursive(n-1) + fibonacciRecursive(n-2); } int main() { int n;...
#include<iostream> using namespace std; int main() { float num,avg,sum=0.0; int count=0; bool moreNumbers=true; cout<<"Enter grades:"<<endl; while(moreNumbers) { cin>>num; if(num < 0) { moreNumbers=false; } else { count = count+1; sum=sum+num; } } avg=sum/count; cout<<"Average grade:"<<avg<<endl; cout<<"How many grades were entered:"<<count<<endl; return 0; } Question: We need to add another loop to check input. Just like the input loop we already have, this one should use a boolean variable as a control. So, the logic could be something like this: Loop...
One dimensional array What this code print #include <iostream> using namespace std; int main () { const int SIZE = 7; int numbers [SIZE] = {1, 2, 4, 8): // Initialize first 4 elements cout << Here are the contents of the array:\n"; for (int index = 0; index < SIZE: index++} cout << numbers[index] << ; cout << endl; return 0; }
#include <iostream> using namespace std; int main() { int sumOdd = 0; // For accumulating odd numbers, init to 0 int sumEven = 0; // For accumulating even numbers, init to 0 int upperbound; // Sum from 1 to this upperbound // Prompt user for an upperbound cout << "Enter the upperbound: "; cin >> upperbound; // Use a loop to repeatedly add 1, 2, 3,..., up to upperbound int number =...
Find and fix errors #include <iostream> #include <cstdlib> #include <ctime> using namespace std; const int MIN = 1; const int MAX = 10; int getRandom(int low, int high); int main() { int random_num = 0; int player_num; int tries; int seed = static_cast<int>(time(0)); bool guessed = false; srand(seed); // call the getRandom function below tries = 4; while ( tries > 0 && !guessed ) { cout << "Enter a number within the range 1 to...
Game of Craps C++ #include <iostream> using namespace std; void roll (int *); // using pointer to catch the random variable value of two dicerolls at one time . //these global variable with static variable win use to operate the working of code . //static win, account because two times input given by user to add the diceroll with win number and //account (means balance of user) . static int account = 100; static int win = 0; int bet...