BG Development


  Reply to this topicStart new topicStart Poll

>  C++
Likulau
: 03-11-2018, 19:50
Quote Post



:
:
:

: 2
: 03.11.18



,

, . :
CODE
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
const int AVERAGE = 1,
          SUM = 2,
          MIN = 3,
          RESULT = 4,
          QUIT = 5;
int choice, input;
long double num = 1, value = 1, sum = 0;
do
{
    cout << "\n\t\tMenu\n\n"
           << "1. Average\n"
           << "2. Sum\n"
           << "3. Min\n"
           << "4. Result\n"
           << "5. Quit\n\n"
           << "Enter your choice: ";
       cin >> choice;
       while (choice < SUM || choice > QUIT)
       {
           cout << "Please enter a valid menu choice: ";
           cin >> choice;

       }
       if (choice != QUIT)
       {
           switch (choice)
           {
       case AVERAGE:
           double array[5], average;
           double average=0;
           cin>>array[0];
           for (int i = 0; i<5; i++){
               average = average+array[i];
           }
           cout<<average/5<<endl;
           break;
       case SUM:
       double sbor = 0;
           cin>>array[0];
           for (int i = 0; i<5; i++){
               sbor = sbor+array[i];
           }
           cout<<sbor<<endl;
           break;
       case MIN:
           double i, score[5], min;
   cout<<"Enter 5 scores:\n";
   cin>>score[0];
   min=score[0];
   for (i=1; i<5; i++)
   {
       cin>>score[i];
       if(score[i]=min)
           min=score[i];
   }
   cout<<"The lowest score is "<<min<<endl;
   break;
       case RESULT:
           cout<<"The vallues in the array "<<endl;
           break;
           else
       {
           cout << "\nThe program has ended. Goodbye.\n";
       }
}
}

}while (choice != QUIT);
   return 0;
}


, , . , , . . icon_smile.gif
PMEmail Poster
Top
Gamma Goblin
: 03-11-2018, 22:53
Quote Post



:
:
:

: 1413
: 21.02.18



, . , -

CODE
use std::io;

fn main() {
   println!("Program start...");

   let mut data = Box::new(vec![]);

   loop {
       println!("0. -> Exit application");
       println!("1. -> Enter data");
       println!("2. -> Find smallest number");

       let choice = enter_number("Enter your choice: ");
       match choice {
           0 => {
               println!("Bye Bye");
               return;
           }
           1 => {
               data = enter_data();
           }
           2 => {
               if data.len() == 0usize {
                   println!("You must enter data using option #1");
               } else {
                   let smallest = find_smallest_number(&data);
                   println!("Smallest number is {} with index {}", smallest.0, smallest.1);
               }
           }
           _ => {
               println!("Invalid choice: {}", choice);
           }
       }
   }
}

fn enter_data() -> Box<Vec<i32>> {
   let number_of_elements = select_number_of_elements();
   let mut data = Box::new(Vec::new());
   for _i in 0..number_of_elements {
       let num = enter_number("Enter value: ");
       data.push(num);
   }
   return data;
}

fn select_number_of_elements() -> i32 {
   loop {
       let num = enter_number("Enter number of elements in the range of [1-25]: ");
       if num <= 0 || num > 25 {
           println!("The entered number is not in the range [1-25]: {}", num);
           continue;
       }

       return num;
   }
}

fn enter_number(msg: &str) -> i32 {
   let stdin = io::stdin();
   loop {
       println!("{}", msg);

       let mut input = String::new();
       if let Err(e) = stdin.read_line(&mut input) {
           panic!("Failed to read from stdin: {}", e);
       };
       let input = input.trim();

       let as_number = input.parse::<i32>();
       return match as_number {
           Err(_) => continue,
           Ok(n) => n
       };
   }
}

fn find_smallest_number(data: &Vec<i32>) -> (i32, u32) {
   let mut min: (i32, u32) = (data[0], 0);

   for index in 1..data.len() {
       if min.0 > data[index] {
           min.0 = data[index];
           min.1 = index as u32;
       }
   }

   return min;
}


--------------------
! ! ! !
, ! , !
---
Raw, and untamed in spirit, We chew this world and Spit it out
---
Challenge my own world to chaos
---
I overcame myself, the sufferer; I carried my own ashes to the mountains; I invented a brighter flame for myself.
PMEmail PosterUsers Website
Top
Gamma Goblin
: 03-11-2018, 23:56
Quote Post



:
:
:

: 1413
: 21.02.18



:

CODE
use std::io::stdin;
use std::io::stdout;
use std::io::Write;

fn main() {
println!("Program start...");

let mut data = None;

loop {
println!("\n0. -> Exit application");
println!("1. -> Enter data");
println!("2. -> Find smallest positive number");
println!("3. -> Find sum on non-zero numbers and number of zeroes");
println!("4. -> Average in interval");

let choice = enter_number("Enter your choice: ");
match choice {
0 => {
println!("Bye Bye");
return;
}
1 => {
data = Some(enter_data());
}

_ => {
let x = match data.as_ref() {
None => {
println!("You must enter data using option #1");
continue;
}
Some(x) => x
};

match choice {
2 => {
match min_positive(x) {
None => println!("There are no positive numbers in the data"),
Some(num) => println!("Smallest positive number is {} with index {}", num.0, num.1)
}
}
3 => {
let sum = non_zero_sum(x);
let zeroes = zero_count(x);
println!("Number of zeroes: {}", zeroes);
println!("Sum: {}", sum);
}
4 => {
let range = select_min_max_range();
let avg = avg_in_range(x, range.0, range.1);
match avg {
None => println!("There are no numbers that match the range [{}, {}]", range.0, range.1),
Some(res) => println!("Average: {}", res)
}
}
_ => {
println!("Invalid choice: {}", choice);
}
}
}
}
}
}

fn enter_data() -> Box<Vec<i32>> {
let number_of_elements = select_number_of_elements();
let mut data = Box::new(Vec::new());
for _i in 0..number_of_elements {
let num = enter_number("Enter value: ");
data.push(num);
}
return data;
}

fn select_number_of_elements() -> i32 {
loop {
let num = enter_number("Enter number of elements in the range of [1-25]: ");
if num <= 0 || num > 25 {
println!("The entered number is not in the range [1-25]: {}", num);
continue;
}

return num;
}
}

fn enter_number(msg: &str) -> i32 {
loop {
print!("{}", msg);
if let Err(e) = stdout().flush() {
panic!("Failed to flush stdout: {}", e);
}

let mut input = String::new();
if let Err(e) = stdin().read_line(&mut input) {
panic!("Failed to read from stdin: {}", e);
};
let input = input.trim();

let as_number = input.parse::<i32>();
return match as_number {
Err(_) => continue,
Ok(n) => n
};
}
}

fn non_zero_sum(data: &Vec<i32>) -> i32 {
let mut sum = 0;
for x in data {
if *x != 0 {
sum += x;
}
}

return sum;
}

fn zero_count(data: &Vec<i32>) -> i32 {
let mut zeroes = 0;
for x in data {
if *x == 0 {
zeroes += 1;
}
}
return zeroes;
}

fn min_positive(data: &Vec<i32>) -> Option<(i32, usize)> {
let mut min = None;

for index in 1..data.len() {
if data[index] > 0 {
match min {
None => min = Some((data[index], index)),
Some(m) => {
if m.0 > data[index] {
min = Some((data[index], index));
}
}
}
}
}

return min;
}

fn select_min_max_range() -> (i32, i32) {
loop {
println!("Enter range [min, max]:");
let min = enter_number("Min: ");
let max = enter_number("Max: ");

if min <= max {
return (min, max);
}
}
}

fn avg_in_range(data: &Vec<i32>, min: i32, max: i32) -> Option<f64> {
if min > max {
panic!("Min [{}] cannot be larger than max [{}]", min, max);
}

let mut sum = 0;
let mut count = 0;

for x in data {
if *x <= max && *x >= min {
sum += *x;
count += 1;
}
}

if 0 == count {
None
} else {
Some(sum as f64 / count as f64)
}
}


QUOTE
/usr/bin/cargo run --color=always --package rust-tutorial --bin rust-tutorial
  Compiling rust-tutorial v0.1.0 (file:///home/******/CLionProjects/rust-tutorial)
Finished dev [unoptimized + debuginfo] target(s) in 0.46s
  Running `target/debug/rust-tutorial`
Program start...

0. -> Exit application
1. -> Enter data
2. -> Find smallest positive number
3. -> Find sum on non-zero numbers and number of zeroes
4. -> Average in interval
Enter your choice: 1
Enter number of elements in the range of [1-25]: 4
Enter value: 9
Enter value: -1
Enter value: 0
Enter value: 5

0. -> Exit application
1. -> Enter data
2. -> Find smallest positive number
3. -> Find sum on non-zero numbers and number of zeroes
4. -> Average in interval
Enter your choice: 2
Smallest positive number is 5 with index 3

0. -> Exit application
1. -> Enter data
2. -> Find smallest positive number
3. -> Find sum on non-zero numbers and number of zeroes
4. -> Average in interval
Enter your choice: 3
Number of zeroes: 1
Sum: 14

0. -> Exit application
1. -> Enter data
2. -> Find smallest positive number
3. -> Find sum on non-zero numbers and number of zeroes
4. -> Average in interval
Enter your choice: 4
Enter range [min, max]:
Min: 1
Max: 10
Average: 7

0. -> Exit application
1. -> Enter data
2. -> Find smallest positive number
3. -> Find sum on non-zero numbers and number of zeroes
4. -> Average in interval
Enter your choice:


Gamma Goblin 03-11-2018, 23:57


--------------------
! ! ! !
, ! , !
---
Raw, and untamed in spirit, We chew this world and Spit it out
---
Challenge my own world to chaos
---
I overcame myself, the sufferer; I carried my own ashes to the mountains; I invented a brighter flame for myself.
PMEmail PosterUsers Website
Top
Likulau
: 04-11-2018, 01:34
Quote Post



:
:
:

: 2
: 03.11.18



. , .
PMEmail Poster
Top
1 (1 , 0 )
, :
« | C/C++ | »

Topic Options Reply to this topicStart new topicStart Poll

 


Copyright © 2003-2018 | BG Development | All Rights Reserved
RSS 2.0