I want to print a chart by taking integers from console. I have come up with this syntax. Stuck at a conditional statement.
int main() {
int array[10];
int num;
int size = 0;
for(int i = 0; i < 10; i++)
{
cin >> num;
if(num == 0)
{
break;
}
array[i] = num;
size++;
}
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
if(*something goes here*)
cout << "*";
else
cout << " ";
}
cout << endl;
}
return 0;
}
Try this
#include <iostream>
using namespace std;
#define MAX 100
int main() {
int values[MAX];
int size = 0;
while (true) {
int num;
cin >> num;
if (num == 0) break;
else values[size++] = num;
}
for (int i = 0; i < size; i++) {
for (int j = 0; j < values[i]; j++) {
cout << "*";
}
cout << endl;
}
return 0;
}
Related
What's wrong with my code
I Have to add all the edge values of the 2D Array.
The Function is not returning what I want. Its returning garbage value.
Program Req Output : Add all the edges value of the 2D Array and return its answer.
You are not allowed to use Vector etc.
Here is my Code :
#include <iostream>
using namespace std;
const int rows = 3;
const int cols = 4;
void AddEdges (int arr[rows][cols])
{
int sum = 0;
int totalOfEdges = 0;
int i,j;
int k = 0;
int l = 0;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= cols; j++)
{
if (i == 1 || i == rows || j == 1 || j == cols)
{
sum = arr[k][l];
totalOfEdges = sum + totalOfEdges;
}
l++;
}
k++;
}
cout << "Total of Edges = " << totalOfEdges << endl;
}
int main()
{
int arr[3][4] = {0};
cout << "Enter Elements for Array = \n";
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cin >> arr[i][j];
}
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
AddEdges(arr);
}
My Output :
Leaving the Correct Code in Case Any one need it .
#include <iostream>
using namespace std;
const int rows = 3;
const int cols = 4;
void AddEdges (int arr[rows][cols])
{
int sum = 0;
int totalOfEdges = 0;
int i,j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
if (i == 0 || i == (rows-1) || j == 0 || j == (cols-1))
{
sum = arr[i][j];
totalOfEdges = sum + totalOfEdges;
}
}
}
cout << "Total of Edges = " << totalOfEdges << endl;
}
int main()
{
int arr[3][4] = {0};
cout << "Enter Elements for Array = \n";
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cin >> arr[i][j];
}
}
cout << "--------" << endl;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
AddEdges(arr);
}
I need to enter 2 integers, n and k, n is the range and k is the amount of divisors for those numbers, and only display the numbers with k divisors.
#include <iostream>
using namespace std;
int main()
{
int n, k,cnt=0;
cin>>n;
cin>>k;
for(int i=1; i<=n; i++)
{
if(n%i==0)
{
cnt++;
}
if(k==cnt)
cout<<i<<" ";
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main(){
int n, k;
cin>>n>>k;
for(int i = 1; i <= n; i++){
int num_divisors_i = 0;
for (int j = 1, len = sqrt(i); j <= len; j++) {
if (i % j == 0) {
if (i / j == j) {
num_divisors_i++;
}
else {
num_divisors_i = num_divisors_i + 2;
}
}
}
if(num_divisors_i == k) cout<<i<<" has "<<k<<" divisors"<<endl;
}
}
This was my solution:
#include<iostream>
int main(int argc, char* argv[]) {
int n = 0, k = 0;
std::cout << "Enter the range: ";
std::cin >> n; if (!std::cin) throw std::runtime_error("range read failed");
std::cout << std::endl << "Enter the number divisors: ";
std::cin >> k; if (!std::cin) throw std::runtime_error("number of divisors read failed");
std::cout << "numbers with " << k << " divisor:: ";
for (int i = 1; i != n+1/*if k is inclusive else n*/; ++i) {
int cnt = 0;
for (int j = 1; j != i+1; ++j) {
if (i % j == 0)
cnt += 1;
}
if(cnt == k)
std::cout << i << ' ';
}
return 0;
}
I am new to C++, and am trying to perform a matrix multiplication taking data from a file. But I am unable to get the multiplication part. Someone please help me to fix this.
While am trying to do the multiplication of 2 matrices but am unable to get the output.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string line;
int d[3][3],e[3][3],f[3][3];
int i=0;
int x=0;
int j=0;
string a[20];
string b[20];
string c[20];
ifstream myfile;
myfile.open("numeric.txt");
while(getline (myfile,line))
{
if(line!=" ")
{
a[x]=line;
b[x]=line;
cout<<a[x]<<endl;
x++;
}
}
cout<<"first Matrix"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<=4;j++)
{
cout<<a[i][j]<<"";
}
cout<<endl;
}
cout<<"Second Matrix"<<endl;
for(i=1;i<4;i++)
{
for(j=0;j<6;j++)
{
cout<<b[i][j]<<"";
}
cout<<endl;
}
cout<<"Multiplication"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<=3;j++)
{
c[i][j]=0;
for(int k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
cout<<"Multiplication Result"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<c[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
Here is a complete example on how you write (fixed size) matrices to a file and how you can build them reading from that file:
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
int main() {
const size_t matrixSize = 3;
//
ofstream matrixOutput("matrix.txt");
for (size_t i = 0; i < matrixSize; i++) {
for (size_t j = 0; j < matrixSize; j++) {
matrixOutput << j*i << ' ';
}
matrixOutput << '\n';
}
for (size_t i = 0; i < matrixSize; i++) {
for (size_t j = 0; j < matrixSize; j++) {
matrixOutput << j*i * 2 << ' ';
}
matrixOutput << '\n';
}
matrixOutput.close();
//
ifstream matrixData("matrix.txt");
size_t matrixInput[matrixSize][matrixSize];
size_t matrixInput2[matrixSize][matrixSize];
size_t position = 0;
size_t number = 0;
while (matrixData >> number) {
const size_t matrixNumber = size_t(floor(position / (matrixSize*matrixSize)));
const size_t row = size_t(floor(position / matrixSize)) % matrixSize;
switch (matrixNumber) {
case 0:
matrixInput[row][position % 3] = number; break;
case 1:
matrixInput2[row][position % 3] = number; break;
}
position++;
}
matrixData.close();
cout << "Matrices: " << endl;
for (size_t i = 0; i < matrixSize; i++) {
for (size_t j = 0; j < matrixSize; j++) {
cout << matrixInput[i][j] << ' ';
}
cout << endl;
}
cout << endl;
for (size_t i = 0; i < matrixSize; i++) {
for (size_t j = 0; j < matrixSize; j++) {
cout << matrixInput2[i][j] << ' ';
}
cout << endl;
}
cout << endl;
cout << "Matrices multiplication: " << endl;
for (size_t i = 0; i < matrixSize; i++) {
for (size_t j = 0; j < matrixSize; j++) {
cout << matrixInput[i][j] * matrixInput2[i][j] << ' ';
}
cout << endl;
}
// ...
}
Note: if you want you can write the matrix size to the file too, so you can retrieve it later and build custom size matrices.
First i need to re-arrange all the values of my array into ascending order then add it afterwards. For example the user input 9 2 6, it will display in ascending order first ( 2 6 9 ) before it will add the sum 2 8 17.. The problem is my ascending order is not working, is there something wrong in my code?
#include <iostream>
#include<conio.h>
using namespace std;
int numberof_array, value[10], temp;
int i = 0, j;
void input()
{
cout << "Enter number of array:";
cin >> numberof_array;
for (i = 0; i < numberof_array; i++)
{
cout << "Enter value for array [" << i + 1 << "] - ";
cin >> value[i];
cout << endl;
}
}
void computation()
{
// this is where i'll put all the computation
for (j = 0; j < numberof_array; j++)
{
cout << value[j];
}
for (i = 0; i <= numberof_array; i++)
{
for (j = 0; j <= numberof_array - i; j++)
{
if (value[j] > value[j + 1])
{
temp = value[j];
value[j] = value[j + 1];
value[j + 1] = temp;
}
}
}
}
void display()
{
// display all the computation i've got
cout << "\nData after sorting: ";
for (j = 0; j < numberof_array; j++)
{
cout << value[j];
}
getch();
}
int main()
{
input();
computation();
display();
}
void computation(){
for (int j = 0; j < numberof_array; j++) cout << value[j]<<"\t";
for (int i = 0; i <= numberof_array; i++) {
temp = value[i];
int temp_idx = i;
for (int j = i; j < numberof_array; j++) {
if (value[j] < temp) {
temp = value[j];
temp_idx = j;
}
}
int temp_swap = value[i];
value[i] = value[temp_idx];
value[temp_idx] = temp_swap;
}
}
How about changing your second function to something like above.
I have to agree with other commentators that your coding style is not preferred but there might be more to the story than meets the eye.
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
}
}