Is my code wrong or has the app crash? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 4

Is my code wrong or has the app crash?

I'm doing project 3 of c++ (ticket) and every time I give him run any of the examples it randomly fails. This is the code: #include <iostream> using namespace std; int main() { int ages[5]; int menor = ages[0]; for (int i = 0; i < 5; ++i) { cin >> ages[i]; if (menor > ages[i]) { menor = ages[i]; } } float porcentaje = menor * 0.5; float final = 50 - porcentaje ; cout << final << endl; return 0; } Can someone explain to me if any part is wrong or why is this happening to me?

6th Jan 2021, 12:15 AM
Rucasu
Rucasu - avatar
12 Antworten
+ 6
you initialized 'menor' with an uninitialized value 'ages[0]' I will suggest you to initialize it in the for loop, at the first iteration: so add this in your for loop, after cin >>ages[i]; if(i==0) menor=ages[i];
6th Jan 2021, 12:23 AM
John Robotane
John Robotane - avatar
+ 2
you initialized 'menor' with an uninitialized value 'ages[0]' I will suggest you to initialize it in the for loop, at the first iteration: so add this in your for loop, after cin >>ages[i]; if(i==0) menor=ages[i];
7th Jan 2021, 11:35 PM
ᙢᖇ.ᚪ ᚱ ع ℰ 爪 Ꭿ ກ
ᙢᖇ.ᚪ ᚱ ع ℰ 爪 Ꭿ ກ - avatar
+ 1
I said after cin>>ages[i]; not before! it should look like this: cin>>ages[i]; if(i==0) menor = ages[i]; ....
6th Jan 2021, 12:51 AM
John Robotane
John Robotane - avatar
+ 1
Oh, thanks. The code already works and I get it. Seriously thank you very much.
6th Jan 2021, 12:54 AM
Rucasu
Rucasu - avatar
+ 1
you're welcome, keep practicing!
6th Jan 2021, 1:05 AM
John Robotane
John Robotane - avatar
+ 1
Thank you, this code also works and it looks like the project was designed to be the result but the other one also served me.
6th Jan 2021, 1:33 AM
Rucasu
Rucasu - avatar
+ 1
Helou
6th Jan 2021, 9:25 PM
Maicon Douglas
Maicon Douglas - avatar
0
Thank you very much for the help but it has not solved the problem. Randomly some of the project examples continue to fail with numbers like 8.46362e+08.
6th Jan 2021, 12:35 AM
Rucasu
Rucasu - avatar
0
can you share your code link?
6th Jan 2021, 12:38 AM
John Robotane
John Robotane - avatar
- 1
<html lang="en" manifest="webogram.appcache" ng-csp="" xmlns:ng="http://angularjs.org" id="ng-app" style="display:background: rgb(231, 235, 240);"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> <title>Telegram Web</title> <link rel="stylesheet" href="css/app.css"> <link rel="manifest" href="manifest.webapp.json"> <link rel="icon" href="favicon.ico" type="image/x-icon"> <link rel="apple-touch-icon" href="img/iphone_home120.png"> <link rel="apple-touch-icon" sizes="120x120" href="img/iphone_home120.png"> <link rel="apple-touch-startup-image" media="(device-width: 320px)" href="img/iphone_startup.png"> <meta name="apple-mobile-web-app-title" content="Telegram Web"> <meta name="mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"> <meta name="theme-color" content="#497495"> <m
7th Jan 2021, 5:49 PM
Yoni Mnil
Yoni Mnil - avatar
- 2
<html lang="en" manifest="webogram.appcache" ng-csp="" xmlns:ng="http://angularjs.org" id="ng-app" style="display: block; background: rgb(231, 235, 240);"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> <title>Telegram Web</title> <link rel="stylesheet" href="css/app.css"> <link rel="manifest" href="manifest.webapp.json"> <link rel="icon" href="favicon.ico" type="image/x-icon"> <link rel="apple-touch-icon" href="img/iphone_home120.png"> <link rel="apple-touch-icon" sizes="120x120" href="img/iphone_home120.png"> <link rel="apple-touch-startup-image" media="(device-width: 320px)" href="img/iphone_startup.png"> <meta name="apple-mobile-web-app-title" content="Telegram Web"> <meta name="mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"> <meta name="theme-color" content="#49749
7th Jan 2021, 2:26 PM
Yoni Mnil
Yoni Mnil - avatar