I'm working on homework for my c++ class we have just started arrays. I cannot figure out why the function reverse_array() does not print to the console when the function show_array() prints to the console without an issue. I've attempted googling the issue without success. I am just beginning c++ so it could be something small that I'm overlooking,. I appreciate any help.
#include<iostream>
using namespace std;
double dbval[5];
void fill_array(int x, double dbval[]);
void show_array(int x, double dbval[]);
void reverse_array(int x, double dbval[]);
int main(){
int x = 0;
fill_array(x,dbval);
show_array(x,dbval);
reverse_array(x,dbval);
return 0;
}
void fill_array(int x, double dbval[]){
int count = 0;
for (x = 0; x < 5; x++){
cin >> dbval[x];
if(!cin){
break;
}
}
for(x = 0; x < 5; x++){
count = count + 1;
}
cout << "Entries " <<int(count);
cout <<endl;
}
void show_array(int x, double dbval[]){
for (x = 0; x<5;x++){
cout << dbval[x] << " ";
}
cout << endl;
}
void reverse_array(int x, double dbval[]){
for(x = 5; x < 5; x--){
cout << dbval[x] << " ";
}
cout << endl;
}
This loop for(x = 5; x < 5 ; x--) failed because it did not satisfy your condition the first time it checked (x = 5, but it needs to be smaller than 5 to enter the loop)
Change to this:
void reverse_array(int x, double dbval[]){
for(x = 4; x >= 0 ; x--){
cout << dbval[x] << " ";
}
cout << endl;
}
Related
For my class, I am needing to write a program using arrays instead of individual variables that contain the score. Struggling with this chapter. I'll leave my code below. Any help would be greatly appreciated. Needing to find the sum between all of the scores entered, subtract the highest and lowest and then find the average.
#include <iostream>
#include <iomanip>
using namespace std;
// Function prototypes
void getJudgeData(double, double);
double calcScore(double);
double findLowest(double);
double findHighest(double);
int main()
{
// Declare variables
const int SIZE = 7;
double scores[SIZE];
double sum;
for (int x = 0; x < SIZE; x++)
double getJudgeData(scores[x]);
return 0;
}
// Function getJudgeData
void getJudgeData(double &judgeScore, double scores[])
{
static int judge = 1;
cout << "Enter score " << judge << ": ";
cin >> judgeScore;
while (judgeScore < 0 || judgeScore > 10)
{
cout << "Invalid score, please enter a score between 0 and 10";
cout << "Enter score " << judge << ": ";
cin >> judgeScore;
}
judge++;
return;
}
// Function calcScore
double calcScore(double scores[])
{
double highest = findHighest(scores);
double lowest = findLowest(scores);
double result = 0;
for (int x = 0; x < 7; x++)
{
result += scores[x];
}
result - findHighest(scores) - findLowest(scores);
cout << fixed << showpoint << setprecision(2);
cout << "The average after the dropping the highest and lowest scores: " << result / 5 << endl;
return result;
}
// Function findLowest
double findLowest(double scores[])
{
double result = 10;
for (int x = 0; x < 7; x++)
{
if (scores[x] < result)
result = scores[x];
}
return result;
}
// Function findHighest
double findHighest(double scores[])
{
double result = 0;
for (int x = 0; x < 7; x++)
{
if (scores[x] > result)
result = scores[x];
}
return result;
}
I want to display an inverted half-star pyramid pattern next to each other.Here's the desired output
And here's my code:
#include <iostream>
using namespace std;
int main()
{
int n, x, y, k;
cout << "Enter Number of Rows: ";
cin >> n;
for (x = n; x >= 1; x--)
{
for (y = 1; y <= x; y++)
{
if (y <= x)
cout << "*";
else
cout << " ";
}
for (y = n; y >= 1; y--)
{
if (y <= x)
cout << "*";
else
cout << " ";
}
cout << "\n";
}
return 0;
}
Here's the output I got after running the code.
The number of rows desired is 10.
After running my code, the output isn't like what I expected. Please tell me how to make it right.
Thank you.
I saw some symmetries in the problem
for n rows, we're printing 2*n+1 characters
for the yth row, we're printing an asterisk if x is less than n-y or more than n+y
So I coded a single double loop with the more complex if statement. I had to adjusted the if statement until it worked.
#include <iostream>
using namespace std;
int main()
{
int n, x, y;
cout << "Enter Number of Rows: ";
cin >> n;
for (y = 0; y < n; y++)
{
for (x = 2*n+1; x > 0; x--)
{
if ((x > n+y+1) || (x < n-y+1))
cout << "*";
else
cout << " ";
}
cout << "\n";
}
return 0;
}
Well, you need to change your logic a little bit rest all is fine.
Here is the code:-
#include <iostream>
using namespace std;
int main()
{
int n, x, y, k;
cout << "Enter the number of Rows: ";
cin >> n;
for(x = 1; x <= n; x++)
{
for(y = n; y >= 1; y--)
{
if(y <= x)
cout << " ";
else
cout << "*";
}
for(y = 1; y <= n; y++)
{
if(y <= x)
cout << " ";
else
cout << "*";
}
cout << "\n";
}
return 0;
}
Explanation:- I am starting from row 1 and going till row "n". Now I need to print two different inverted flags so I used two for loops for that. In one loop I am starting from column "n" and going till row >1 and in the other loop, I am doing the just opposite of that so that both flags will be opposite to each other. Just try to understand this code by taking X as row and Y as column.
So me and my group have been working on this project for a few days we can get the top part too draw when we step into it but it does not run and we are not sure why it wont.
The first function is to draw the top and the second is to draw the bottom.
The top seems to draw fine i am not sure why the code will not even run.
#include<iostream>
using namespace std;
void drawTopPart(int userNum);
void drawBottomPart(int userNum);
int main() {
//declarations
int userNum;
//get user input
cout << "Enter an odd number from 1 to 15: ";
cin >> userNum;
//output
drawTopPart(userNum);
drawBottomPart(userNum);
cout << endl;
system("pause");
return 0;
}
void drawTopPart(int userNumPar) {
int z = 1;
int size;
cin >> size;
for (int i = 0; i <= size; i++) {
for (int j = size; j>i; j--) {
cout << " ";
}
cout << "*";
if (i>0) {
for (int k = 1; k <= z; k++) {
cout << " ";
} z += 2; cout << "*";
}
cout << endl;
}
}
void drawBottomPart(int userNumPar){
int z -= 4;
int size;
cin >> size;
for (int i = 0; i <= size - 1; i++) {
for (int j = 0; j <= i; j++) {
cout << " ";
}
cout << "*";
for (int k = 1; k <= z; k++) {
cout << " ";
}
z -= 2;
if (i != size - 1) {
cout << "*";
}
}
You have two mainly errors.
First,
void drawBottomPart(int userNumPar){
int z -= 4;
int size;
int z -= 4; have syntax error.You can change it int z = 17;
Second,you forget endl here.
if (i != size - 1) {
cout << "*";
}
cout << endl;
#include <iostream>
int main()
{
std::cout << " Enter the small number " << std::endl;
int v1 = 0;
std::cin >> v1;
std::cout << " Enter the large number " << std::endl;
int v2 = 0;
std::cin >> v2;
int x = 0;
while (x <= v2-1) {
x = v1+1;
++x;
}
std::cout << x << std::endl;
return 0;
}
This code gives no error but i couldn't get any output after input of two numbers. But, it works with a for loop like this;
{
for (int x = v1+1; x <= v2-1; ++x)
std::cout << x << std::endl;
return 0;
}
what is the mistake i should avoid using while loop ?
int x = 0;
while (x <= v2-1) {
x = v1+1;
++x;}
You get an infinite loop here, the value of x is always either v1+1, or v1+2.
use this:
int x = v1+1;
while (x++ <= v2-1) {
cout<<x;
}
I have a program that should print out 2 lists like the one bellow, basically the same list but backwards, however it works the first time but then it prints some weird output which I Will show bellow as well.
0123456789
9876543210
however the actual output I get from my program is this:
Can anyone tell me what is wrong with my code please, I am not sure why I am getting this output.
void createFile(){
ofstream myfile;
myfile.open ("TEST1.txt");
for(int x = 0; x < 10; x++){
myfile << x << "\n";
}
myfile.close();
}
void popArray(int array1[]){
ifstream infile("TEST1.txt");
int x;
while(infile >> array1[x]){
cout << array1[x];
}
}
void reverseList(int array2[]){
for(int x = 9; x > -1; x--){
cout << setw(2) << array2[x];
}
}
void checkLists(int array1[], int array2[], int sizeOfArray){
for(int x = array1[1]; x < sizeOfArray; x++){
for(int y = array2[1]; x < sizeOfArray; x++){
if(x == y)
cout << "Palindrome" << endl;
else{
cout << "Not" << endl;
}
}
}
}
int main()
{
int array1[10];
int array2[10];
createFile();
popArray(array1);
cout << "\n";
reverseList(array1);
cout << "\n";
checkLists(array1, array2, 10);
}
This is the problem I think:
void popArray(int array1[]){
ifstream infile("TEST1.txt");
int x;
while(infile >> array1[x]){
cout << array1[x];
}
}
You never specify what x is. If I understand what you're trying to do I think this should work:
void popArray(int array1[]){
ifstream infile("TEST1.txt");
int x;
for (x=0; x < 10; x++){
cout << array1[x];
}
}
Just read through a bit more. You've also got errors here:
void checkLists(int array1[], int array2[], int sizeOfArray){
for(int x = array1[1]; x < sizeOfArray; x++){
for(int y = array2[1]; x < sizeOfArray; x++){
if(x == y)
cout << "Palindrome" << endl;
else{
cout << "Not" << endl;
}
}
}
}
I'd do something like:
bool checkLists(int array1[], int array2[], int sizeOfArray){
bool isPalindrome=true;
for(int x = 0; x < sizeOfArray; x++){
if(array1[x] != array2[sizeOfArray-(x+1)]){
isPalindrome = false;
}
}
return isPalindrome;
}
Then at the end of main you could have:
if(checkLists(array1, array2, 10)){
cout << "Is Palindrome\n";
}
else{
cout << "Is Not Palindrome\n";
}
While I'm at it I might as well fix this too:
void reverseList(int array2[]){
for(int x = 9; x > -1; x--){
cout << setw(2) << array2[x];
}
}
Change this to:
void reverseList(int array1[], int array2[]){
for(int i = 0; i < 10; i++){
array2[9-i] = array1[i];
}
}
I think if you put together all the bits of my answer you should more or less have something that works. I've not tested it myself though.
void popArray(int array1[]){
ifstream infile("TEST1.txt");
int x;
while(infile >> array1[x]){
cout << array1[x];
}
}
You are not changing x in your loop (or initializing x at all!)
for(int x = array1[1]; x < sizeOfArray; x++){
for(int y = array2[1]; x < sizeOfArray; x++){if(x == y)
cout << "Palindrome" << endl;
else{
cout << "Not" << endl;
}
}
i this you should replace you code by this one
for(int x = 0; x < sizeOfArray; x++){
for(int y = 0; y < sizeOfArray; y++){
if(array1[x]== array2[y])
cout << "Palindrome" << endl;
else{
cout << "Not" << endl;
}
}