I created a program to show the sum and show the reversed number a person has typed. The sum function works but the revers function is not. Can anyone give me any tips on how to fix it.
I created a program to show the sum and show the reversed number a person has typed. The sum function works but the revers function is not. Can anyone give me any tips on how to fix it.
#include<iostream>
#include<iomanip>
using namespace std;
void printSum(int n, bool reverse);
int sm(int n);
int reverseInt(int n);
void printAddTable(int n);
int main()
{
int reverse;
int sum=0;
int n;
cout<<"Enter N value:"<<endl;
cin>>n;
if(n>0)
{
reverse = true;
printSum( n, reverse); // calls the printSum Method
}
else
{
//cout<<"enter positive Number only:"<<endl;
}
sum = sm(n); //err // calls the sum Method
reverse = reverseInt(n); // calls the reverseInt Method
cout<<"The reverse value is:"<<reverse;
printAddTable(n); // calls the printAddTable Method
//getch()
}
//end of main()
void printSum(int n, bool reverse)
{
int sum=0;
// print sum of reverse numbers
for(int i=n; i>=1; i--)
{
sum=sum+i;
cout<<i<< " "<<"+"<<" ";
}
cout<<sum;
}
int sm(int n)
{int sum =0;
for(int i=1; i<=n; i++)
{
sum = sum + i ;
cout << endl;
cout<<i<<" "<<"+"<<" "<<endl; // print n positive integers
cout << endl;
}
cout<< "Are " <<n<< " positive integers"<<endl;
cout<< "Sum is "<<sum <<endl;
return sum;
}
int reverseInt(int n)
{
int reminder=0;
int sum =0;
while(n<=0)
{
reminder = n/10;
sum = (sum * 10) + reminder; // it returns the reverse number
n = n % 10;
}
return sum;
}
void printAddTable(int n)
{
for(int i=1; i<=n; i++)
{
cout<<i<<endl;
for(int j=1; j<=n; j++) // print n X n add table
{
cout<<i+j<<endl;
}
}
}
{
In your code, following is to be changed in reverseInt function as mentioned by the comment.
while(n<0)
{
//reminder = n/10; should be
reminder = n%10;
sum = (sum * 10) + reminder; // it returns the reverse number
//n = n % 10;
//Should be
n = n/10; //or n /= 10;
}
Related
This the the function that store the value in sum for digitsum of all n digit number
void findsum(int pos,int n,int& sum)
{
if(pos>n){
return;
}
for (int i = 0; i < 10; i++)
{
findsum(pos+1,n,sum);
sum+=i;
}
}
This is the driver function
int main()
{
int n;
cin>>n;
int sum=0;
findsum(1,n,sum);
cout<<sum;
}
You wanted the output as the sum of the digits that you are giving as input.
Input
Enter the Number:1234
Output
10
Here is your code
import java.io.*;
class Sum {
static int getSum(int n)
{
int sum = 0;
while (n != 0) {
sum = sum + n % 10;
n = n / 10;
}
return sum;
}
public static void main(String[] args)
{
int n ;
cin>>n;
System.out.println(getSum(n));
}
}
For my CS class we are making a program that reads in numbers, calculates the inflation rate from those 2 numbers, sends those numbers to an array, bubblesorts the array, and then prints that array out after it is sorted. However, I can't get my bubblesort to sort, either there is a compiler error when I use &, or there it just doesnt sort. Can anyone help? Thank you!
#include
using namespace std;
double InflationRate(float old_cpi, float new_cpi);
double inflateRate;
void getCPIValues(float &old_cpi, float &new_cpi);
float swap_values(float&i, float&j);
void bubbleSort(float a[], int number_used);
float old_cpi=-1;
float new_cpi=-1;
const int MAX_RATES=20;
float a[MAX_RATES]={};
int main() //C++ programs start by executing the function main
{
char Continue = 'y';
double total = 0;
int i=0;
do{
getCPIValues(old_cpi, new_cpi);
InflationRate(old_cpi, new_cpi);
cout<< "Inflation rate is "<<inflateRate<<endl;
total = total+inflateRate;
cout << "Try again? (y or Y): ";
i++;
a[i-1]= inflateRate;
cin >> Continue;
}while ((Continue == 'y')||(Continue == 'Y'));
cout<<"Average rate is "<<total/i<<endl;
int number_used= i-1;
for (int p; p<=number_used; p++)
{
cout<<a[p]<<endl;
}
return 0;
}
double InflationRate(float old_cpi, float new_cpi)
{
inflateRate = (new_cpi - old_cpi)/old_cpi*100;
return(inflateRate);
}
void getCPIValues(float &old_cpi, float &new_cpi)
{
cout<<"Enter the old and new consumer price indices: ";
cin>>old_cpi>>new_cpi;
if ((old_cpi<=0)||(new_cpi<=0))
{
do
{
cout<<"Error: CPI values must be greater than 0";
cin>>old_cpi>>new_cpi;
}while ((old_cpi<=0)||(new_cpi<=0));
}
}
float swap_values(float&i, float&j)
{
int temp = i;
i=j;
j=temp;
return(i, j);
}
void bubbleSort(float a[], int number_used)
{
for (int m = 0; m < number_used-1; m++)
for (int n = 0; n < number_used-m-1; n++)
if (a[n] > a[n+1])
swap_values(&a[n], &a[n+1]);
}
This is what I wrote so far, where did I go wrong?
#include <iostream>
using namespace std;
int main()
{
int x[5], count=0;
for (int i=0;i<5;i++)
cin>>x[i];
for (int i=0;i<5;i++)
{
for (int n=2;n<x[i];n++)
{
if (x[i]%n==0)
count++;
}
if (count==1)
cout<<x[i]<<" ";
}
}
Edit:
Many thanks to everyone that tried to help. The problem was that I had to int count in the loop so that it would start from 0 every time. Here's my new working code:
#include <iostream>
using namespace std;
int main()
{
int x[5];
for (int i=0;i<5;i++)
cin>>x[i];
for (int i=0;i<5;i++)
{ int count=0;
for (int n=2;n<=x[i];n++)
{
if (x[i]%n==0)
count++;
}
if (count==1)
cout<<x[i];
}
}
#include <iostream>
using namespace std;
int main()
{
int x[5], count=0;
for (int i=0;i<5;i++)
cin>>x[i];
for (int i=0;i<5;i++)
{
bool check = true;
for (int n=2;n<x[i];n++)
{
if (x[i]%n==0)
{
check = false;
break;
}
}
if (check)
cout<<x[i]<<" ";
}
}
For better complexity, change your nested for loop to :
for (int n=2;n<=sqrt(x[i]);n++)
Other way :
int main ()
{
for (int i=2; i<100; i++)
{
bool check=true;
for (int n=2; n*n<=x[i]; n++)
{
if (x[i] % n == 0)
{
check=false;
break;
}
}
if(check) cout << x[i] << " ";
}
return 0;
}
Here's my solution.
I basically assume all numbers are prime, but if I find them not to be prime I don't print them out.
int main() {
bool primeNumber;
int length = 0;
int x[5], count=0;
for (int i=0;i<5;i++){
length++;
cin>>x[i];
}
for (int i = 0; i < length; i++ )
{
primeNumber = true;
for (int j = 3; j <= x[i]/2; j += 2 )//Not necessary to check agains numbers higher than half the number you want to check
{
if (x[i] % j == 0)
{
primeNumber = false;
}
}
if(primeNumber){cout << "Primenumber: " << x[i] << endl;}
}
}
EDIT:
Here's a comment on your code
Let's say your array looks like this
{5,9,13,15, 17}
int main()
{
int x[5], count=0;
for (int i=0;i<5;i++)
cin>>x[i];
for (int i=0;i<5;i++)
{
for (int n=2;n<x[i];n++)
{
Here you loop from 2-4(2,3,4). It's only necessary to loop half the number you want to check. e.g. (x[i]/2), but you can do it like you have done here.
if (x[i]%n==0){
count++;}
When your number is 5 and you check agains n numbers This will produce 5%2=1,5%3=2,5%4=1. But 5 is a prime number and your code didn't manage to detect that.
}
if (count==1)
Let's say you did find that 5 is a prime above and you managed to increment count to 1. What happens when you find another prime number in your array e.g. 7. That will make count be equal to 2. But you only check against 1, so therefor your next prime number doesn't get printed because count will only be equal to 1 after the first prime number and not the rest.
cout<<x[i]<<" ";
}
}
Hopefully you understood my explanation. It always helps to go over your code by hand or debug mode and think what the code will do with different numbers.
All numers are divideable by itself. That will make all numbers prime if you try to divide numbers with themselves
So the question is to Add selection sort function to grade program above. Program should display list of grades in sorted ascending order, we are giving the selection sort function and cant change it, my question is how would I call it from the main function
Here is my code`
#include <iostream>
using namespace std;
double average(double x[], int n);
double maximum(double x[], int n);
double minimum(double x[], int n);
int nAboveAvg(double x[], int n);
void sort(double x[], int npts);
int main()
{
double grades[50];
int ngrades;
cout<<"How many grades? (max = 50) ";
cin>>ngrades;
//create for loop to get grades from user
for(int i = 0; i<ngrades; i++)
{
cout<<"Enter grade ";
cin>> grades[i];
while(grades[i]< 0 || grades[i] > 100)
{
cout<<"Invalid grade- please enter again"<<endl;
cin>>grades[i];
}
}
//call the functions
double avg = average(grades, ngrades);
double max = maximum(grades, ngrades);
double min = minimum(grades, ngrades);
int nAbove = nAboveAvg(grades, ngrades);
//Calling the sort function
sor = sort(grades, ngrades);
//display results
cout << "Average = " << avg << endl;
cout << "# above average = " << nAbove << endl;
cout<<"Max value is = "<<max<<endl;
cout<<"Min value is = "<<min<<endl;
cout<<"Array sorted "<<sor<<endl;
}
void sort(double x[], int npts)
{
double min_value;
int min_index;
double temp;
for(int i= 0; i<npts - 1; i++)
{
for(int j = i + 1; j<npts; j++)
{
if(x[j] < min_value)
{
min_value = x[i];
min_index = j;
}
}
temp = x[min_index];
x[min_index] = x[i];
x[i] = temp;
}
return;
}
`
I think your problem is that you expect the "sort" function to return a value; it does not.
The "sort" function does not return a value, because it was defined with a "void" return value therefore trying to retrieve any data from the variable "sort" will not work (or should not, anyway).
Arrays are passed-in to functions by reference; This means that all of the changes done to the array within the sort function are are still there once the function returns; because of this, you should be outputting the "grades" array, not a non-existent return value.
EDIT: I believe that your problem is at the line:
cout<<"Array sorted "<<sor<<endl;
Trying something like this instead:
for (int i = 0; i < ngrades; ++i)
{
cout << grades[i] << " ";
}
cout << endl;
EDIT 2: Also, change the line:
sor = sort(grades, ngrades);
to just:
sort(grades, ngrades);
EDIT 3: It turns out that there are a few problems with the "sort" function. The first, and worst, problem is that the variable "min_value" is being used without being defined.
Once I changed this, the program would run, but the "sort" function did not work properly.
This brings me to the second problem: The variables "min_value" and "min_index" need to be reset for every iteration of "i".
The final problem is that, within the "j" loop, "min_value" is assigned to "x[i]", whereas it should be assigned to "x[j]":
min_value = x[i];
min_index = j;
should be:
min_value = x[j];
min_index = j;
I fixed the function and tested it to make sure that it works.
Here is the code.
void sort(double x[], int npts)
{
double min_value;
int min_index;
double temp;
for (int i = 0; i < npts - 1; i++)
{
min_value = x[i];
min_index = i;
for (int j = i + 1; j < npts; j++)
{
if (x[j] < min_value)
{
min_value = x[j];
min_index = j;
}
}
temp = x[min_index];
x[min_index] = x[i];
x[i] = temp;
}
return;
}
So the question is to figure out whether the array is ascending or not ascending meaning that if the array is 2,3,4,6 it would display yes it is ascending, if it is 3,2,5,6 it would display no not ascending.
I'm cant seem to get the program to display a 0 which mean not ascending and I Know the issue is somewhere in the for loop or if statement but I don't know how to fix it
#include <iostream>
using namespace std;
int ascending(int x[], int npts);
int main()
{
int number[10];
int num;
cout<<"How many numbers would you like to enter? (max = 10) ";
cin>>num;
for(int i = 0; i<num; i++)
{
cout<<"Enter number ";
cin>>number[i];
}
int asc = ascending(number, num);
cout<<asc;
}
int ascending(int x[], int n)
{
int count = 0;
for(int i = 0; i<n-1; i++)
{
if(x[i]<x[i + 1])
count++;
if(count <= n - 1)
return 1;
else
return 0;
}
}
Your issue is in the ascending function, you're returning 1 after the first iteration. In order to determine if the sequence of numbers is ascending, you must iterate through all of them. However,if you find a number that's not ascending at any time, then you can return 0 right away. If you safely iterate through all them without returning 0, than you know the sequence is ascending
int ascending(int x[], int n)
{
for(int i = 0; i<n-1; i++)
{
if(x[i]>=x[i + 1])
return 0
}
return 1
}
int ascending(int x[], int n)
{
int i = 0;
Do
{
if(x[i]> x[i + 1])
return 0;
i++;
} while (i < n-1);
return 1;
}