C++ simple matrix - c++

can someone fix my code?
this is the result that should be showed when i input number 5 in c++
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
my result:
1
2 6
3 7 10
4 8 11 14
5 9 12 15 18
my code:
#include <iostream.h>
#include <conio.h>
#include <math.h>
void main()
{
int n,i,j;
cout<<"insert number"<<endl;
cin>>n;
for (i=1;i<=n;i++)
{
int y=1;
int g=1;
cout<<i<<" ";
for (j=1;j<=i-1;j++)
{
int x=n;
int b=i;
x--;
g--;
cout<<(x*y)+b+g<<" ";
y++;
}
cout<<endl;
}
getch ();
}
what did i do wrong?
sorry if my code messy i'm a c++ new learner.

You could it like this:
#include <iostream>
using namespace std;
int main()
{
int n , i ,j, sum;
cout << "masukkan bilanga" << endl;
cin >> n;
for(i = 0; i < n; i++)
{
cout << i + 1 << " ";
sum = i + 1;
for(j = 0; j < i; j++)
{
sum += n - 1 - j;
cout << sum << " ";
}
cout << endl;
}
return 0;
}
where the key point is that you want to print all the numbers, starting from 1, in a column-major manner, until a triangular n x n matrix is created.
Driven from the output, one can easily see that every element of the next column is what the current element is, plus n - 1, and that factor decreases by one as we advance to the right part of the matrix.

Try this code, hope this is what you need:
#include <iostream>
#include <string>
void printMatrix(int number);
void main()
{
int number = 1;
std::cout << "Enter a number: ";
std::cin >> number;
printMatrix(number);
}
void printMatrix(int number) {
std::cout << std::endl;
for (int i = 1; i <= number; i++) {
std::cout << i << " ";
int n = i;
for(int j = 1; j < i; j++) {
n += number - j;
std::cout << n << " ";
}
std::cout << std::endl;
}
}

Related

Program to print Factorial of a number in c++

Q) Write a program that defines and tests a factorial function. The factorial of a number is the product of all whole numbers from 1 to N.
For example, the factorial of 5 is 1 * 2 * 3 * 4 * 5 = 120
Problem: I am able to print the result,but not able to print like this :
let n = 5
Output : 1 * 2 * 3 * 4 * 5 = 120;
My Code:
# include <bits/stdc++.h>
using namespace std;
int Factorial (int N)
{
int i = 0;int fact = 1;
while (i < N && N > 0) // Time Complexity O(N)
{
fact *= ++i;
}
return fact;
}
int main()
{
int n;cin >> n;
cout << Factorial(n) << endl;
return 0;
}
I am able to print the result,but not able to print like this : let n
= 5 Output : 1 * 2 * 3 * 4 * 5 = 120;
That's indeed what your code is doing. You only print the result.
If you want to print every integer from 1 to N before you print the result you need more cout calls or another way to manipulate the output.
This should only be an idea this is far away from being a good example but it should do the job.
int main()
{
int n;cin >> n;
std::cout << "Factorial of " << n << "!\n";
for (int i =1; i<=n; i++)
{
if(i != n)
std::cout << i << " * ";
else
std::cout << n << " = ";
}
cout << Factorial(n) << endl;
return 0;
}
Better approach using std::string and std::stringstream
#include <string>
#include <sstream>
using namespace std;
int main()
{
int n;
cin >> n;
stringstream sStr;
sStr << "Factorial of " << n << " = ";
for (int i = 1; i <= n; i++)
{
if (i != n)
sStr << i << " * ";
else
sStr << i << " = ";
}
sStr << Factorial(n) << endl;
cout << sStr.str();
return 0;
}

What other thing i can use except array here?

is there any way to get rid of arrays in this program? Im not allowed to do it with std::array or std::vector.
#include <iostream>
using namespace std;
int main()
{
int upper,i,j=0,k=0;
int arr1[1000],arr2[1000];
cout<<"Enter the upper bound :";
cin>>upper;
for(i=0 ; i<upper ; i++)
{
if(i%2 == 0)
{
arr1[j] = i;
j++;
}
else
{
arr2[k] = i;
k++;
}
}
cout<<"List of even numbers :";
for(i = 0; i<j ; i++)
{
cout<<arr1[i]<<" ";
}
cout<<"\n";
cout<<"List of odd numbers :";
for(i = 0; i<k ; i++)
{
cout<<arr2[i]<<" ";
}
return 0;
}
Instead of setting elements of two arrays just output at first even numbers in the given range and then odd numbers.
For example
#include <iostream>
int main()
{
std::cout << "Enter the upper bound: " ;
unsigned int n = 0;
std::cin >> n;
std::cout << "List of even numbers :";
for ( unsigned int i = 0; i < n; i += 2 )
{
std::cout << i << ' ';
}
std::cout << '\n';
std::cout << "List of odd numbers :";
for ( unsigned int i = 1; i < n; i += 2 )
{
std::cout << i << ' ';
}
std::cout << '\n';
return 0;
}
The program output might look like
Enter the upper bound: 10
List of even numbers :0 2 4 6 8
List of odd numbers :1 3 5 7 9
Quick and dirty solution.
#include <iostream>
int main()
{
int upper;
std::cout << "Enter the upper bound :";
std::cin >> upper;
std::cout<<"List of even numbers :";
for (int i=0; i<upper;i+=2)
std::cout <<i<<" ";
std::cout <<"\n";
std::cout<<"List of odd numbers :";
for (int i=1; i<upper;i+=2)
std::cout <<i<<" ";
return 0;
}
I'll not analyze the rest of your code but focus on the question:
is there any way to get rid of arrays in this program?
Since you can't use std::vector<int> you could allocate the memory dynamically yourself.
#include <cstddef>
#include <iostream>
#include <memory>
int main()
{
size_t upper;
std::cout << "Enter the upper bound :";
if(not (std::cin >> upper)) return 1; // input failed, exit
// create unique_ptr<int[]> poiting to an array of "upper" number of elements:
auto arr1 = std::make_unique<int[]>(upper);
auto arr2 = std::make_unique<int[]>(upper);
// ...

pointers with numbers in backwards [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I need to receive an undetermined number of integer numbers in between (0-9). With this numbers, print them forwards and backwards, and then erase the numbers at the corners.
Example:
3 5 1 9 4 6 2 4 4 2 6 4 9 1 5 3
5 1 9 4 6 2 4 4 2 6 4 9 1 5
1 9 4 6 2 4 4 2 6 4 9 1
9 4 6 2 4 4 2 6 4 9
4 6 2 4 4 2 6 4
6 2 4 4 2 6
2 4 4 2
4 4
And here is the code I have so far:
#include <iostream>
using namespace std;
int a;
int p;
int set;
void numberss()
{
for set[](int a=0; a<p; a++)
}
int main()
{
cin >> p;
cin >> a;
const int SIZE = p;
int set[] = {a};
int *numPtr;
numPtr = set;
for (int index = 0; index < SIZE; index++)
{
cout << *numPtr << " ";
numPtr++;
}
for (int index = 0; index < SIZE; index++)
{
numPtr--;
cout << *numPtr << " ";
}
return 0;
}
If we ignore errors, you can read the numbers in one at a time, and form a string for the first line of output. Forming the string will involve appending a reversed copy to the original. Once the string is formed, you can output that string for the first line. Then replace the first number with a space character, and shrink the string from the back by two characters. Keep doing that until you are done.
This works because the numbers are all single digit.
int main (void)
{
int N;
std::string nums;
std::cin >> N;
for (int i = 0, x; i < N; ++i) {
std::cin >> x;
nums += std::to_string(x) + ' ';
}
nums.append(nums.rbegin() + 1, nums.rend());
for (int i = 0; i < N; ++i) {
std::cout << nums << '\n';
nums[2*i] = ' ';
nums.resize(nums.size()-2);
}
}
DEMO
Your code doesn't work because you are not reading all of the numbers from the user's input, you are only reading the count and the 1st number. Also, you are not looping enough times to output the numbers in a triangular fashion, you are only outputting the 1st line of the triangle.
Try this instead:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int p;
vector<int> set;
cin >> p;
set.resize(p);
for (int i = 0; i < p; ++i)
cin >> set[i];
for (int index = 0; index < p; index++)
{
int *numPtr = &set[index];
for (int i = 0; i < index; ++i)
cout << " ";
for (int i = index; i < p; ++i)
cout << *numPtr++ << " ";
for (int i = index; i < p; i++)
cout << *--numPtr << " ";
cout << endl;
}
return 0;
}
Live Demo
That being said, here is an alternative approach that is more C++-ish and less C-ish, by using iterators instead of pointers, and using STL algorithms. Also, you should always validate user input before using it:
#include <iostream>
#include <vector>
#include <iomanip>
#include <algorithm>
#include <limits>
#include <iterator>
#include <cstdint>
using uint16vec = std::vector<uint16_t>; // there is no operator>> for uint8_t...
int main()
{
size_t count = 0;
std::cin >> count;
uint16vec set;
set.reserve(count);
for (size_t i = 0; i < count; ++i)
{
uint16vec::value_type num;
while (!((std::cin >> num) && (num <= 9)))
{
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.clear();
std::cout << "Enter a valid number 0..9!" << std::endl;
}
set.push_back(num);
}
auto begin = set.begin(), end = set.end();
auto rbegin = set.rbegin(), rend = set.rend();
auto out = std::ostream_iterator<uint16vec::value_type>(std::cout, " ");
std::cout << std::setfill(' ');
for (size_t i = 0; i < count; ++i)
{
std::cout << std::setw((i*2)+1);
std::copy(begin++, end, out);
std::copy(rbegin, rend--, out);
std::cout << std::endl;
}
return 0;
}
Live Demo
Give this a shot, mate. I've commented it so you can follow along. It allows for characters other than numbers because that seemingly wasn't a requirement of your assignment, so it's on you to sort and filter those out. It prints as intended, so here's hoping that this is what you're looking for to start off with your search for the right answer.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void pop_front(vector<char>& _vector)
{
_vector.front() = move(_vector.back());
_vector.pop_back();
}
int main()
{
vector<char> characterList;
string consoleInput;
cin >> consoleInput;
//Initialize by pushing everyting from the console into our vector.
for (char c : consoleInput)
{
characterList.push_back(c);
}
//After adding from the console, now we'll do it in reverse.
for (int i = consoleInput.length() - 1; i > -1; i--)
{
characterList.push_back(consoleInput[i]);
}
//Print where we are currently up to for display purposes.
for (char c : characterList)
{
cout << c;
}
// Newline.
cout << "\n";
//Now let's start to chop it down. It'll take the same iterations as the console input, because we're doubling down.
for (int i = 0; i < consoleInput.length() - 1; i++)
{
characterList.erase(characterList.begin());
characterList.pop_back();
for (char c : characterList)
{
cout << c;
}
cout << "\n";
}
return 0;
}

Using an Array[10] and incrementing it with a While loop using modulo 10 to Pair User inputs together

#include <iostream>
using namespace std;
int main()
{
long int number;
int digits;
cout << "Enter Number: ";
cin >> number;
int counter[10] = { 0,0,0,0,0,0,0,0,0,0 };
while (number != 0) {
digits = number % 10;
counter[digits] = counter[digits] + 1;
number = number / 10;
}
for (int i = 0; i<10; i++) {
if (counter[i] != 0) {
cout << i << ": " << counter[i] << endl;
}
}
return 0;
system("pause");
}
I'm having an issue with my code that when I run it and enter a Number nothing really happens. It is supposed to run something like 1234556789 and the output should look like
1 : 9
2 : 8
3 : 7
4 : 6
5 : 5
I know sometimes if there isn't a system pause this happens where it runs part of the code and just ends, but I'm not sure whats wrong here.
#include <iostream>
using namespace std;
int main()
{
long int number;
int digits;
cout << "Enter Number: ";
cin >> number;
int counter[10]={0},a=0;
while (number != 0) {
digits = number % 10;
counter[a] = digits; //made changes to this line
number = number / 10;
++a;
}
for (int i = 0; i<10; i++) {
if (counter[i] != 0) {
cout << i << ": " << counter[i] << endl;
}
}
return 0;
}
All you are doing right now is printing how many digits there are of each number 0-9 in the number. If you want to pair elements together, then you can use std::vector and iterators. The number of digits in your input can be either even or odd and you would have to account for both cases.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
long int number;
cout << "Enter Number: ";
cin >> number;
vector<int> digits;
if (number == 0)
{
digits.push_back(number);
}
while (number != 0)
{
digits.push_back(number % 10);
number /= 10;
}
auto it_begin = digits.begin();
auto it_end = digits.end() - 1;
if (digits.size() % 2 == 1)
{
for (; it_end != it_begin; ++it_begin, --it_end)
{
cout << *it_end << ": " << *it_begin << endl;
}
cout << *it_end << endl;
}
else
{
for (; it_begin < it_end; ++it_begin, --it_end)
{
cout << *it_end << ": " << *it_begin << endl;
}
}
}
With number = 1234556789, the output is:
1: 9
2: 8
3: 7
4: 6
5: 5
If you want first 10 no. Only use this code
include using namespace std;int main() {long int number;cout << "Enter Number: ";cin >> number;for (int i = 1; i<=10; i++) {cout << i << ": "<

Vector Appending

so far this is my code what i am trying to do is say the user inputs 1 2 3 and then presses -1, he or she will be asked to input another set of numbers say 9 8 7, what my programs is suppose to do is display them out as such 1 2 3 9 8 7, but rather it is displaying them like this 6 6 6 6 6 6, basically it counts how many numbers there are and displays that amount of numbers with that number. So can anyone help me out here, how do i make it so that it displays the two sets of numbers combined?
#include <iostream>
#include <vector>
using namespace std;
vector<int> append(vector<int> a, vector<int> b)
{
int n = a.size();
int m = b.size();
vector<int> c(n + m);
int i;
for (i = 0; i < n; i++)
c[i] = a[i];
for (i = 0; i < m; i++)
c[n + i] = b[i];
return c;
}
int main()
{
cout << "Please enter a set of numbers, insert -1 when done.\n";
vector<int>a;
bool more = true;
while (more)
{
int n;
cin >> n;
if (n == -1)
more = false;
else
a.push_back(n);
}
cout << "Please enter another set of numbers, insert -1 when done.\n";
vector<int>b;
more = true;
while (more)
{
int m;
cin >> m;
if (m == -1)
more = false;
else
b.push_back(m);
}
vector<int>d = append(a,b);
{
int i;
for (i= 0; i < d.size(); i++)
cout << d.size() << "\n";
}
}
That's because at the end you're printing the size, not the value:
cout << d.size() << "\n";
Should be:
cout << d[i] << "\n";
It is because when you are printing it, you are printing d.size instead of d[i].
cout << d.size() << "\n";
Would needs to be:
cout << d[i] << endl;