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.
Related
I have a question about the exercise from my course:
Write a program that takes array of real numbers as parameter and replaces each element that is smaller than average of the first and last element, by this average. This is my code:
#include <iostream>
#include <string>
using namespace std;
void replaverage(int arr[], int n)
{
for (int i; i < 6; i++) {
cout << "Enter the numbers" << endl;
cin >> arr[i];
}
int f = arr[0];
int l = arr[n - 1];
double av = f + l / 2;
for (int i; i < n; i++) {
if (arr[i] < av) {
arr[i] = av;
}
}
}
int main()
{
int n;
int arr[n];
replaverage(arr, n);
cout << arr << " " << endl;
return 0;
}
Code is working, however as an output, it is giving some kind of address "0x7fff2306a5c0". I'm beginner so I apologize, maybe my error is stupid but I don't know how to fix it. Thanks for helping!
#include <iostream>
#include <string>
using namespace std;
void replaverage(int arr[], int n)
{
for (int i = 0; i < n; i++) {
cout << "Enter the number: ";
cin >> arr[i];
cout << endl;
}
int f = arr[0];
int l = arr[n - 1];
double av = (f + l) / 2;
for (int i = 0; i < n; i++) {
if (arr[i] < av) {
arr[i] = av;
}
}
}
int main()
{
int n = 6; // Making 6 since you had it hardcoded
int arr[n];
replaverage(arr, n);
for (int i = 0; i < n; i++) {
cout << arr[i] << endl;
}
return 0;
}
First problem: Initialize your loop counters to 0;
Second problem: Initialize n in main being passed as parameter to
something
Third problem: Your average calculation is incorrect. It should be (f+l) / 2. Otherwise it will be doing l/2 + f, which is incorrect.
Fourth problem: You need to loop over your array to see all the
elements
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.
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
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;
}
i'm trying to write a program that reads numbers from a text file( 21 12 44 21 -5 63 0 ) to an array and bubble sort them in an descending order, and printing out only positive numbers. i have been trying for a while but what is displayed is not what i expect.
the contents of the text file are:
21 12 44 21 -5 63 0
the full code:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <conio.h>
using namespace std;
class bubble
{
public:
unsigned* arr;
int n;
//Constructor
bubble(const int& size) : n(size) { this->arr = new unsigned[n]; }
//function to read from file
void inputvf(istream &f)
{
//check if file is open
if (f.fail()) {
cout << "\n Error in openning file!!!";
exit(1);
}
for (int i = 0; i < n; i++)
f >> arr[i];
delete[] arr;
//close file
//f.close();
}
//Bubble sort function
void bubblesort()
{
for (int i = 1; i<n; i++)//for n-1 passes
{
for (int j = 0; j<n - 1; j++)
{
if (arr[j] < arr[j + 1])
{
int temp;
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void display()
{
cout << endl;
cout << "----------------------\n";
cout << "Sorted array elements \n";
cout << "----------------------\n";
if (arr >= 0){
for (int j = 0; j < n; j++)
cout << arr[j] << endl;
}
}
};
int _tmain(int argc, _TCHAR* argv[])
{
bubble list(7);
ifstream file("Text.txt");
list.inputvf(file);
list.bubblesort();
list.display();
_getch();
return 0;
results after i run the code:
4277075694
1
4261281277
2880154539
2880154539
4277075694
0
what am i doing wrong??? please help
this is the new code(below):
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <conio.h>
using namespace std;
class bubble
{
public:
//Array of integers to hold values
int* arr;
//Number of elements in array
int n;
//Constructor
bubble(const int& size) : n(size) { this->arr = new int[n]; }
//function to read from file
void inputvf(istream &f)
{
//check if file is open
if (f.fail()) {
cout << "\n Error in openning file!!!";
exit(1);
}
for (int i = 0; i < n; i++)
f >> arr[i];
//delete[] arr;
//close file
//f.close();
}
//Bubble sort function
void bubblesort()
{
for (int i = 1; i<n; i++)//for n-1 passes
{
for (int j = 0; j<n - 1; j++)
{
if (arr[j] < arr[j + 1])
{
int temp;
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void display()
{
cout << endl;
cout << "----------------------\n";
cout << "Sorted array elements \n";
cout << "----------------------\n";
if (arr >= 0){
for (int j = 0; j < n; j++)
cout << arr[j] << endl;
}
}
};
int _tmain(int argc, _TCHAR* argv[])
{
bubble list(7);
ifstream file("Text.txt");
list.inputvf(file);
list.bubblesort();
list.display();
_getch();
return 0;
}
Two problems:
In inputvf:
delete[] arr;
You should not delete the array at this point - you haven't even started using it yet.
This declaration:
unsigned* arr;
means that all of your input is unsigned, which means -1 is read as 4294967291 and thus will be treated as a large number. Change your array to a normal int and then use an if test to ignore negative numbers when outputting.