Hey am trying to run this code but the for loop executes only once.
Removing the line cout< fixes the problem but I need the ans to be precise to 15 decimal places.
here's the code
int n,i;
cin>>n;
double a[n];
for(i=0;i<n;i++){
cin>>a[i];
a[i]=(a[i]/100);
cout<<fixed<<setprecision(15)<<a[i];
}
for(i=0;i<n;i++) {
cout<<a[i];
}
First things first
Use of variable length arrays (VLAs) is not standard C++. It is supported by some compilers as an extension. If you are allowed to use std::vector, use
std::vector<double> a(n);
If you are not allowed to use std::vector, use
double* a = new double[n];
and make sure to use
delete [] a;
before the end of the funtion.
Suggestion to help you diagnose the problem
Add appropriate prompts before entering data and messages to indicate what was read.
for(i=0;i<n;i++)
{
cout << "Enter number for a[" << i << "]: ";
cin>>a[i];
a[i]=(a[i]/100);
// Make sure to add endl at the very end.
cout<< "Value of a[" << i << "]: " << fixed << setprecision(15) << a[i] << endl;
}
Related
I'm new to the site. I'm sorry for my missing. When I call the print_array function it gives an error. I do not understand why this is happening. Could you please help me?
Thank you for answers.
#include <iostream>
using namespace std;
void print_array(int n,int m,int matrix[][m]){
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cout << matrix[i][j];
}
cout << "\n";
}
cout << "\n";
}
int main() {
int m,n;
while (true){
cout << "Given row size: " ;
cin >> n ;
cout << "Given column size: ";
cin >> m;
if (m>=5 && n>=5){
break;
}
else{
cout <<"Row and Column size must be grater than 5. Please try again"<<endl;
continue;
}
}
int array_A[n][m];
cout <<"Enter elements by giving a space between them\n"<<endl;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin >> array_A[i][j];
}
}
cout << "\n";
print_array(n,m,array_A);
return 0;
}
I think your understanding of array allocation is a bit wrong. In C++, you can't just dynamically allocate memory for an array as you did with int array_A[n][m]. You either have to allocate memory manually using the keyword new or use a container that does this for you, like std::vector. Since you are starting I'd go with the standard library container.
You could initialize your variable with something like std::vector<std::vector<int>> array_A(n, std::vector<int>(m, 0)) after including #include <vector>. After this, you could simply use your variable as you did and pass it as a parameter of your function.
I have this code and its job is to ask the user if how many elements they would like to enter in the array - the user then enters their desired elements - the elements are integers -then the program solves for the sum of the entered numbers.
Here's the code:
#include <iostream>
using namespace std;
int main()
{
int arr[20], i, n, sum=0;
cout<<"How many elements you want to enter?: ";
cin>>n;
cout<<"Enter any "<<n<<" elements in Array: ";
for(i=0;i<n;i++)
cout<<"How many elements you want to enter?: ";
cin>>n;
cout<<"Enter any "<<n<<" elements in Array: ";
for(i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"Sum of all Elements are: ";
for(i=0;i<n;i++)
{
sum=sum+arr[i];
}
for(i=0;i<n;i++)
{
cout<<sum;
getch();
}
I doesn't seem to work and the program doesn't run, so I can't put the actual output.
Expected output would be the sum of all the elements (integers) that the user entered.
Does it have to be an array? you can do it in a simpler way like this:
cout << "Enter number of elements\n";
cin >> n
cout << "Please enter the elements \n"
int temp = 0;
int sum = 0;
for(int i = 0; i<n ; i++){
cin >> temp;
sum+=temp;
}
cout << "Sum is: " << sum << endl;
One thin you need to note, you did not restrict the user to the max array size .. say he/she said 21 then you'll have an overflow which produces error.
Since you're using C++, I would suggest you go with vectors, or use the simple way I stated above.
Basic advice: design as you go, and at each stage, use a print-out to tell you what values are entered.
cin >> n;
cout << "Please enter " << n << " values to sum: " << endl;
for(i=0;i<n;i++)
{
cin >> arr[i];
cout << "value #" << i << ": " << arr[i] << " entered." << endl;
}
You basically want to move on to the next stage of processing only when you're sure that your program works for entering the initial data. Take each stage of processing one at a time, and output what the current stage is. This makes it much easier to work out which stage of processing is causing an error. You can cut back on the print commands later on once it's working as intended, but I'd recommend leave then in there but commented out. Sometimes incorrect inputs cause errors again, and it's handy to re-enable the debug prints to tell you what's going on.
Here is the program for your problem description.It takes input from the user to how much elements you want to enter into the array then it take elements and saved in an array then calculates the sum of array elements.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int arr[20],i,n,sum=0;
cout<<"How many elements you want to enter: ";
cin>>n;
cout<<"Enter any "<<n<<" elements in Array: ";
for(i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"Sum of all Elements are: " << endl;
for(i=0;i<n;i++)
{
sum=sum+arr[i];
cout << "sum of a[" << i << "] = " << sum << endl;
}
cout<< "the sum of all the elements (integers) that the user entered = " << sum;
getch();
return 0;
}
Now this program gives the information sum of a[i] elements is and so on to the end of the loop.
It is helpful to show the output or error message.
Try to convert cin into an integer or float for:
cin>>arr[i];
There is my friend's code. It works, but we would like to ask our user how many times he wants to type informations. Simplier, We don't know how to ask my user for N ("const int N = 3" line). We've tried changing "const int" into "int", but then an error shows up "expression must have a constant value".
#include <iostream>
using namespace std;
struct T_dane_ksiazki
{
char imie[15];
char nazwisko[30];
char tytul[45];
int rokwydania;
int nrwydania;
};
void WCZYTAJ_dane(T_dane_ksiazki& dane) /*wczytanie informacji o książce*/
{
cout << "\nimie autora: ";
cin >> dane.imie;
cout << "nazwisko autora: ";
cin >> dane.nazwisko;
cout << "tytul ksiazki: ";
cin >> dane.tytul;
cout << "rok wydania: ";
cin >> dane.rokwydania;
cout << "numer wydania: ";
cin >> dane.nrwydania;
}
void WYSWIETL_dane(T_dane_ksiazki dane) /*wczytanie informacji o książce*/
{
cout << "\nimie autora: " << dane.imie;
cout << "\nnazwisko autora: " << dane.nazwisko;
cout << "\ntytul ksiazki: " << dane.tytul;
cout << "\nrok wydania: " << dane.rokwydania;
cout << "\nnumer wydania: " << dane.nrwydania << "\n";
}
const int N = 3;
int Zapytajka()
{
cout << N<< "\n";
return 1;
}
int main()
{
T_dane_ksiazki daneq[N];
Zapytajka();
cout << "Podaj informacje o ksiazkach: \n";
for (int i = 0; i<N; i++)
{
WCZYTAJ_dane(daneq[i]);
}
cout << "\n\nInformacje o ksiazkch: \n";
for (int i = 0; i<N; i++)
{
WYSWIETL_dane(daneq[i]);
}
cout << "\nKoniec programu. Nacisnij ENTER";
cin.ignore(); cin.get();
return 1;
}
C++ only supports arrays whose size is constant. However, if you want a non-constant size, you can use vector, which was designed specifically for this purpose.
Example:
#include <vector>
...
int main()
{
Zapytajka();
cout << "Podaj informacje o ksiazkach: \n";
int n;
cin >> n;
std::vector<T_dane_ksiazki> daneq(N); // moved here and modified
for (int i = 0; i<N; i++)
{
WCZYTAJ_dane(daneq[i]);
}
...
}
I changed an array to a vector, and moved its definition to after the value of n is determined. I also changed N to n because it's no longer a constant, and it's a common convention to allocate lower-case names to variables.
Edit: I missed that it is a C++ question. The answer for C++ is that you should use a C++ container, probably a vector. These containers are the reason that the variable length arrays introduced in C in 1999 discussed below do not exist in C++: There is no real need for them. (There is a discussion whether to introduce something like it but it has non-trivial implications for the type system.)
You can have variable length arrays in C99 programs (for gcc: compile with "-std=c99"). That is, you can make N a non-const and the program should still compile and run properly (I didn't check every detail, but it looks pretty straightforward).
In pre-99 C you have to allocate dynamically with malloc or simply define an array which is big enough for the biggest conceivable number and use only part of it.
So as I explained in the title i'm having trouble trying to get the sum of an array. I've just now learned how to create dynamic arrays and I did some searching on how to calculate the sum. I don't believe I fully understand what is going on to calculate the sum.
// Final Grade Calculator
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main(){
double minor, quiz, major;
int minorG, quizG, majorG;
minorG = 0;
cout << "Final Grade Calculator" << endl;
cout << "Input minor grade weight percent." << endl;
cin >>minor;
cout << "Input quiz grade weight percent." << endl;
cin >>quiz;
cout << "Input major grade weight percent." << endl;
cin >>major;
// Three grade categories
minor = minor/100;
quiz = quiz/100;
major = major/100;
for(int i = 1; i <=10; i++){
cout << "Input a minor grade. (Max=10)" << endl;
cin >>minorG;
int *minorGA = new int[minorG];
minorG+= minorGA[minorG];
cout << "Currently: " << i << " Grade(s)." <<endl;
}
cout << "Minor Sum: " << minorG << endl;
return 0;
}
This is what I have so far and the trouble I am having is within the for loop which is where my array is and where I am trying to get the sum of it. When I compile and run I get a sum of 138,427. Any help would be greatly appreciated!
I think you're over-complicating things with dynamic arrays. I'll explain what you're doing, and try to provide help for what I think you're trying to do.
In your code int* minorGA = new int[minorG]; you are allocating memory for minorG amount of ints. There are two problems here:
You are accessing an element outside of the memory you allocated. When you allocate 10 elements, you can access elements 0-9. Trying to access 10 is undefined behaviour (you are trying to access parts of memory that could contain anything).
The values stored in this array are just whatever is in memory, so when you are attempting to increment minorG by the amount of one of these, it's just whatever is in memory at the time.
A separate problem is that you are not deallocating the memory, but some might argue that it isn't really a problem.
You should just be able to have the following to perform what I think you're trying to do:
for (int i = 0; i < 10; ++i)
{
int inputtedNumber = 0;
cout << "Enter a number" << endl;
cin >> inputtedNumber;
// add that number to some tally:
finalTally += inputtedNumber;
}
Or if you are trying to store the elements in an array, you can use the following:
const int maxElements = 10;
int grades[maxElements] = {}; // this will construct all elements with 0. Without this, the elements may contain any number.
for (int i = 0; i < maxElements; ++i)
{
int inputtedNumber = 0;
cout << "Enter a number" << endl;
cin >> inputtedNumber;
// Store the number
grades[i] = inputtedNumber;
}
In saying that, it will be better to use std::vector (knows its size, handles memory for you, can grow):
std::vector<int> grades;
// Allow the user to enter as many numbers as they'd like
for (;;)
{
int input = 0;
cout << "Enter a number" endl;
cin >> input;
// Store the number. Will continue to grow
grades.push_back(input);
}
I just begin to learn C++, for the main method i do:
#include <iostream>
using namespace std;
int main ()
{
int d;
int n;
cout <<"Enter the denominator: " << endl;
cin >> d;
cout <<"Enter the numerator: " << endl;
cin >> n;
cout <<"The result of operation is: " << endl;
cout << (double)n/d << endl;
cout <<"Done";
return 0;
}
It doesn't produce output, but if I delete return 0. I will generate correct output. Shouldn't the main method in C++ always return an integer eventually?
Try cout.flush(); before return.
It forces the buffered data to be send to the output.
I went through your code and everything seems to be right. When I run it, it works fine. If you haven't solved it yet try to cut and past your code into a new project. I know that it sounds stupid but it should work.
I hope this will help you.