Shortening code using functions - c++

I'm new to coding and i have a question about how to shorten code using function. My homework problem says that i need to take out the nested loops and shorten my previous code using functions and i need a little help jump starting. This code displays 100 random hex colors. I apologize for the messy code, still learning.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(){
srand(time(0));
for(int j=0; j<100; j++){
for(int i=0; i<6; i++){
int digit = rand() % 16;
char letter;
if(digit > 9){
letter = digit + 55;
cout << letter;
}
else{
cout << digit;
}
}
cout << endl;
}
return 0;
}

Your problem statement suggests you modify the code as follows, but note that this does not reduce code size, just makes it more readable:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void innerLoop(){
char letter;
int digit;
for (int i = 0; i<6; i++){
digit = rand() % 16;
if (digit > 9){
letter = digit + 55;
cout << letter;
}
else{
cout << digit;
}
}
}
int main(){
srand(time(0));
for (int j = 0; j<100; j++){
innerLoop();
cout << endl;
}
return 0;
}

As an alternative to code frenzy' code change, there's an easier solution (although not necessarily educative) - generating 1000 decimal integers, then converting them to hexadecimal when printing using std::hex.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(){
srand(time(0));
for(int j=0; j<100; j++){
int number = rand() % 1000; // Or whatever your upper limit is.
cout << hex << number << endl;
}
cout << endl;
return 0;
}

The innermost loop is generating a random hexadecimal digit and printing that.
Let's break out the generation of digits into a function:
char random_digit() {
int digit = rand() % 16;
if (digit > 9) {
return digit - 10 + 'A';
}
else {
return digit;
}
}
int main() {
srand(time(0));
for(int j = 0; j < 100; j++) {
for(int i = 0; i < 6; i++){
cout << random_digit();
}
cout << endl;
}
}
Now we have a loop that prints a number of random digits.
We can turn that into a function too.
Let's give it a parameter so we don't need to hard-code the length:
void print_number(int length) {
for (int i= 0; i < length; i++) {
cout << random_digit();
}
cout << endl;
}
// New main
int main() {
srand(time(0));
for(int j = 0; j < 100; j++) {
print_number(6);
}
return 0;
}

Related

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.

Squaring numbers in array, finding the sum and the biggest number

I have a task to square all the elements of array, separate them with ",", then find a sum of the squared array and find the biggest number of it. I managed to square them and find the sum, but I can't find the biggest number and the program is also printing "," at the end of new array.
This is my code:
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int a[10];
int n,sum=0,kiek=0,max=a[0];;
cin>>n;
for(int i=0;i<n;i++)
{cin>>a[i];
a[i]*=a[i];
sum=sum+a[i];
}
for (int i = 0 ; i < n ; i++)
{ cout <<a[i] << ","; }
cout<<endl ;
cout<<"suma " <<sum;
cout<<endl;
for(int i=0;i<10;i++)
{if(max<a[i])
{
max = a[i];
}
}
cout<<"max "<<max;
return 0;
}
This is the screenshot of my program result when I run it
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int a[10];
int n, sum = 0; // Remove some unused variables
// Input //
cin >> n;
for(int i = 0; i < n; i++){
cin >> a[i];
a[i] *= a[i];
sum += a[i];
}
// List a[] and sum //
for (int i = 0 ; i < n - 1 ; i++) {
cout << a[i] << ", ";
}
cout << a[n - 1] << endl; // Just for a little beauty
cout << "suma " << sum << endl;
// Find Max //
int max = a[0]; // max should be declared there,
// because a[0] has not entered data at the first
for(int i = 0; i < n; i++) { // use n, not 10
if(a[i] > max){
max = a[i];
}
}
cout << "max " << max;
return 0;
}
Unchecked.
Please add indents, spaces and comment, this is a good habit.
Comment : If you are going to get the size of the array at run-time, it is better to use STL containers or pointers. Your issue lies here :
---> for(int i=0;i<10;i++)
{if(max<a[i])
Good luck.

Compilator won't stop printing the same, wrong answer

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++)

Using pointer Notation to print an array

I would like to point out some random integers using the regular print function, then print again the same integers using pointer notation. When I use pointer notation I run into some trouble. If anyone could send some tips it'd be much appreciated. If i comment out a specific line of code, the program will compile completely, but not with the outputs I'd like.
#include <iostream>
#include <ctime>
#include <stdlib.h> //srand(), rand()
using namespace std;
void printArray(int[], int);
//void printToday(int , );
int main()
{
int i = 0;
const int SZ = 100;
int myArray[SZ] ={0};
srand(time(0));
int myArrayTotal = 0;
int *thelight;
thelight = myArray;
for (int i = 0; i <=100; i++)
{
myArray[i]= i+rand()%1000 ;
}
cout << "Array Notation:\n\n";
printArray(myArray, SZ);
system("pause");
system("cls");
cout << "Pointer Notation: \n\n";
int k = 0;
for (int i = 0; i < 10; ++i)
{
for (int j = 0; j < 10; ++j)
{
cout<< *(thelight + k)<< "\t";
++k; //if I comment out this line the second part of the program will run, but it isn' the values I want.
} cout<< endl;
}
}
void printArray(int ArrayName[], int ArraySize)
{
int k = 0;
for (int i = 0; i < 10; ++i)
{
for(int j = 0; j < 10 ; ++j)
{
cout << ArrayName[k] << "\t";
++k;
}cout << endl;
}
}
Thank you

Problems with creating lotto ticket

I have to create a program that looks like the printout of a lotto ticket.
Yet I can't seem to get the "mega" number in the right place. We have to generate 5 random numbers between 1 and 56, then one more number between 1 and 44 (the mega number). So its supposed to look like this:
Yet for some reason the mega number always prints before the 5 random numbers generated between 1 and 56.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "cs110a2.h"
using namespace std;
void fillup(int x[], int n, int from, int to)
{
for(int i = 0; i < n; i++)
{
x[i] = RAND(from,to);
}
cout <<" ";
cout << RAND(1,44);
}
int bubble_sort(int x[], int n)
{
for(int i = 0; i < n-1; i++)
{
int temp;
for(int j=i+1; j<n ; j++)
{
if(x[i] > x[j])
{
temp = x[i];
x[i] = x[j];
x[j] = temp;
}
}
}
return(0); //What! why?
}
void print(int x[], int n)
{
for(int i = 0; i < n; i++)
{
cout << x[i] <<" ";
}
cout << endl;
}
int main(int argc, char **argv)
{
srand(time(NULL));
cout <<" Mega" << endl;
for(int i = 0; i < atoi(argv[1]); i++)
{
int lotto[5];
fillup(lotto,5,1,56);
bubble_sort(lotto,5);
print(lotto,5);
}
return(0);
}
Move the last two lines from the fillup function to the end of the print function.
As it is now, you're printing the mega after GENERATING the numbers, not after printing them.
string formatting using the \t ( tab ) to place where it needs to be.