Compilator won't stop printing the same, wrong answer - c++

So I'm supposed to do a one line matrix from a document but the compilator keeps printing wrong answer. Not to mention when replacing one element with the other, the program won't react to the command.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
const char FV[] = "Masyvas.txt";
using namespace std;
int main()
{
int A[10];
int i,j, m, n;
ifstream Mas(FV);
cout << "Parasykite masyvo elemento indekso numeri, pries kuri iterpsite nauja elementa - ";
cin >> n;
cout << "irasykite iterpiamo elemento reiksme - ";
cin >> m;
for (i = 0; i < 5; i++)
{
Mas >> A[i];
}
for ( j = n; j < 5; j++)
{
A[j + 1] = A[j];
A[n] = m;
}
for (i = 0; i < 5; i)
{
cout << " " << A[i] << endl;
}
Mas.close();
return 0;
}

The problem is in this line:
for (i = 0; i < 5; i)
Here you have infinite loop, because condition i < 5 is always true.
It was easy to find it on your side! Use any debugger next time to analyze so simple problems.
So, just replace that line by this line to fix it:
for (i = 0; i < 5; i++)

Related

Replace element in array by average

I have a question about the exercise from my course:
Write a program that takes array of real numbers as parameter and replaces each element that is smaller than average of the first and last element, by this average. This is my code:
#include <iostream>
#include <string>
using namespace std;
void replaverage(int arr[], int n)
{
for (int i; i < 6; i++) {
cout << "Enter the numbers" << endl;
cin >> arr[i];
}
int f = arr[0];
int l = arr[n - 1];
double av = f + l / 2;
for (int i; i < n; i++) {
if (arr[i] < av) {
arr[i] = av;
}
}
}
int main()
{
int n;
int arr[n];
replaverage(arr, n);
cout << arr << " " << endl;
return 0;
}
Code is working, however as an output, it is giving some kind of address "0x7fff2306a5c0". I'm beginner so I apologize, maybe my error is stupid but I don't know how to fix it. Thanks for helping!
#include <iostream>
#include <string>
using namespace std;
void replaverage(int arr[], int n)
{
for (int i = 0; i < n; i++) {
cout << "Enter the number: ";
cin >> arr[i];
cout << endl;
}
int f = arr[0];
int l = arr[n - 1];
double av = (f + l) / 2;
for (int i = 0; i < n; i++) {
if (arr[i] < av) {
arr[i] = av;
}
}
}
int main()
{
int n = 6; // Making 6 since you had it hardcoded
int arr[n];
replaverage(arr, n);
for (int i = 0; i < n; i++) {
cout << arr[i] << endl;
}
return 0;
}
First problem: Initialize your loop counters to 0;
Second problem: Initialize n in main being passed as parameter to
something
Third problem: Your average calculation is incorrect. It should be (f+l) / 2. Otherwise it will be doing l/2 + f, which is incorrect.
Fourth problem: You need to loop over your array to see all the
elements

Inverted Half Triangle using Astrix Problem

Hi I'm trying to get my C++ console to output the first image I've attached. I know you have to like count the spaces in like maybe a for loop and decrement it as you go down the rows..However, I'm not exactly sure on how to go about that, pretty new to c++. The solution I'm currently working on is the exact mirror image of it. Any help would be greatly appreciated.
#include <iostream>
using namespace std;
int main(){
int rows = 10;
for (int i = 0; i <= rows; i++) {
cout << "" << endl;
for (int j = 1; j <= i; ++j) {
cout << "*";
}
}
}
Correct Solution:
My Current Output:
You need an extra for loop before the one that is sending the asterisks, like so that will send the spaces to push the asterisks to the right.
#include <iostream>
using namespace std;
int main(){
int rows = 10;
for (int i = 0; i <= rows; i++) {
for (int j = rows - i; j > 0; j--) {
cout << " ";
}
for (int j = 1; j <= i; ++j) {
cout << "*";
}
cout << endl;
}
}
#include <iostream>
using namespace std;
int main(){
int rows = 10;
for (int i = 0; i <= rows; i++) {
cout << "" << endl; // Mistake here, you are putting a empty string
for (int j = 1; j <= i; ++j) {
cout << "*";
}
}
}
You're almost good. You have missed to put the spaces in order to create the right padding of the stars.

trying online programming first time factorial giving wrong answer

This is a factorial program,I made on a coding site.Kindly help me find why its wrong?
#include <iostream>
using namespace std;
int main()
{
int i, j, f = 1;
int t, a[100];
cin >> t;//enter test cases
for (i = 0; i < t; i++)
cin >> a[i];//enter all the cases one by one
for (i = 0; i < t; i++)
{
for (j = 1; j <= a[i]; j++)
f = f*j;
cout << f;//displays each factorial
}
return 0;
}
You should reinitialize the value of f back to 1 at the start of each test case.
#include <iostream>
using namespace std;
int main()
{
int i, j, f = 1;
int t, a[100];
cin >> t;//enter test cases
for (i = 0; i < t; i++)
cin >> a[i];//enter all the cases one by one
for (i = 0; i < t; i++)
{
f = 1 // add this line
for (j = 1; j <= a[i]; j++)
f = f*j;
cout << f;//displays each factorial
}
return 0;
}
You should reinitialize the value for your factorial variable f each time you are computing the factorial, also you need not use array to store all the asked factorial cases unnecessarily waste of memory.
Here is the optimized solution:
#include <iostream>
using namespace std;
int main()
{
int i, j, f,num;
int t;
cin >> t; //enter test cases
for (i = 0; i < t; i++)
{
cin >> num;
f = 1;
for (j = 1; j <= num; j++) {
f = f * j;
}
//displays each factorial
cout << f << endl;
}
return 0;
}

"Stray \1 in program" error in a C++ sort program

Here's the code of counting sort I implemented in C++:
#include<iostream>
#include<stdlib.h>
using namespace std;
void counting_sort(int [], int, int);
main()
{
int n,k = 0, a[15];
cout << "Enter the number of input: ";
cin >> n;
cout << "\nEnter the elements to be sorted: \n";
for ( int i = 1; i <= n; i++)
{
cin >> a[i];
if(a[i] > k)
{
k = a[i];
}
}
counting_sort(a, k, n);
system("pause");
//getch();
}
void counting_sort(int a[], int k, int n)
{
int i, j;
int b[15], c[100];
for(i = 0; i <= k; i++)
c[i] = 0;
for(j =1; j <= n; j++)
c[a[j]] = c[a[j]] + 1;
for(i = 1; i <= k; i++)
c[i] = c[i] + c[i-1];
for(j = n; j >= 1; j--)
{
b[c[a[j]]] = a[j];
c[a[j]] = c[a[j]] - 1;
}
cout << "\nThe Sorted array is: ";
for(i = 1; i <= n; i++)
cout << b[i] << " " ;
}
There is an error on compilation that states "Stray \1 in program" in Line 3 Col 1. I tried in on Dev-C++ and Ideone. Both show the same error. I also tried copying the code to a new file but in vain. How can I rectify it?
There is an (hidden) invalid character in your code (line:3) which copied along with your code on http://ideone.com/ALbZbr.
Try editing this code. You will see a red dot (invalid character) on the third line.
#include<iostream>
#include<stdlib.h>
using namespace std; . <--
void counting_sort(int [], int, int);
main()
{
Delete this invalid character and your code will eventually run.

print stars as much as the values in the array

I'm trying to make a C++ program start creating an array and takes the values from the user , then print every value + star as much the value is .. Example : the user had entered 5 then the output must be like this
5*****
Input
1
2
3
4
5
6
output
1*
2**
3***
4****
and so on
.. help :(
#include <iostream>
using namespace std;
void main()
{
int arr[10];
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
int x = arr[i];
for (int j = 0; x <= arr[i]; j++)
{
cout<< "*";
}
}
}
And another help please can you give me some useful link to practice on programming to be professional
Your code is wrong. Use the following code:
#include <iostream>
using namespace std;
int main() {
int arr[10];
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
int x = arr[i];
for (int j = 0; j < x; j++){ // your condition was wrong
cout<< "*";
}
cout<<endl; // for better formatting
}
return 0;
}
For edited question
int main() {
int arr[10];
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
}
for (int i = 0; i < 10; i++)
{
int x = arr[i];
cout << x;
for (int j = 0; j < x; j++){ // your condition was wrong
cout << "*";
}
cout << endl;
}
return 0;
}
#include <iostream>
using namespace std;
void main()
{
int nbValues = 10;
int arr[nbValues];
// First recover the values
for (int i = 0; i < nbValues; i++)
{
cin >> arr[i];
}
// Then print the output
for (int i = 0; i < nbValues; i++)
{
int x = arr[i];
cout << x;// Print the number
for (int j = 0; j < x; j++)
{
cout<< "*";// Then print the stars
}
cout << endl;// Then new line
}
}