I'm having some issues on my code to get the highest number on an array of 5 elements, this is my code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
float timerunner1[4];
int x;
int main() {
for(x=1;x<6;x++) {
printf("Give me the time of runner 1: ");
scanf("%f",timerunner1[x]);
}
return 0;
}
This works perfectly, the output is:
Give me the time of runner 1: 14
Give me the time of runner 1: 3
Give me the time of runner 1: 10
Give me the time of runner 1: 5
Give me the time of runner 1: 2
How can I get the highest and lowest number of the array?
Maybe using a for or if.. How?
Thanks!
It doesn't work actually, you need to use the address of operator '&' to store the value in the array.
scanf("%f", &timerunner1[x]);
Also, your array isn't large enough to store the 6 integers that your loop is requiring and subscripting of an array starts at zero and ends at 5 (for 6 elements).
You can then either have another loop AFTER reading all your values to calculate the maximum or calculate it on the fly as below:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
float timerunner1[6];
int x;
float maximum = 0.0f;
int main() {
for (x = 0; x < 6; x++) {
printf("Give me the time of runner 1: ");
scanf("%f", &timerunner1[x]);
maximum = maximum > timerunner1[x] ? maximum : timerunner1[x];
}
printf("%f\n", maximum);
return 0;
}
Also, this code only works on positive values because maximum is initialised to zero and will always be larger than any negative value, if you need negative values, you should be able to experiement and figure that out.
Ok, in this program you will have to load the time of each player manually.
/* StackFlow
Find the highest of an array of 5 numbers */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void) {
float timerunner1[ 5 ] ={ 0 };
float highest;
int highestindex, i;
/* Data input*/
for( i = 0; i < 5; i++ ){
printf( "\nEnter the %d element of the array: ", i );
scanf( %f, timerunner1[ i ] );
}
/* Considering that the first generated number is the highest*/
highest = timerunner1[ 0 ];
highestindex = 0;
/* The first element of an array is [0] not [1]*/
for( i = 1; i < 5; i++ ) {
/* if the next element in the array is higher than the previous*/
if ( highest < timerunner1[ i ]){
highest = timerunner1[ i ];
highestindex = i;
}
}
printf("\nThe highest time of the runner %d is: %f \n", highestindex, highest);
return 1;
}
Related
Program takes an integer input num from the keyboard and computes the sum of square of i for all I from 1 to num (inclusive).
#include <iostream>
#include <math.h>
int main()
{
int num;
int total;
printf("Please enter a number:");
scanf("%d", &num);
for (double i = 1; i <= num; i++) {
total += (i*i);
printf("%d", total);
}
}
The code above compiles correctly, but when inputting 5 it prints 15143055. Why is it doing this?
#include <iostream> is c++. #include <math> is not used. total is uninitialized. Comparing floating point values may not behave the way you want, so using a (unsigned) integer type instead as a loop counter. Loop values by convention start at 0 instead of 1 in c (you could increment i fist thing in the loop to avoid the double (i+1) but this will be optimized out anyways). Also, your loop not run for a negative value so just require unsigned values. As you sum integers the result ought to be an integer, but double would give you a larger range so I left it as such. Missing return:
#include <stdio.h>
int main() {
unsigned num;
printf("Please enter a number: ");
scanf("%u", &num);
double total = 0.0;
for (unsigned i = 0; i < num; i++) {
total += (i+1)*(i+1);
}
printf("%lf\n", total);
return 0;
}
and the resulting output:
Please enter a number: 3
14.000000
My code:
// DICE ROLL PROGRAM
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
int main()
{
// defining variables until "till here" comment
int i;
int rollDice;
int firInp;
int secInp;
int flag = 1; // flag variable set to 1 which will later be set to zero
srand (time(NULL)); // seeding rand so that we get different values every time
// till here
while(flag) // while loop runs as long as flag variable has a value
{
printf("Enter the amount of faces you want your dice to have (MAX=24, MIN=2): "); // prints the message
scanf("%d", &firInp); // user input stored into firInp
printf("Enter the amount of throws you want(MAX=499, MIN=2): "); // this message is printed after the users first input
scanf("%d", &secInp); // user input stored into secInp
if (((firInp < 25)&&(firInp > 1))&&((secInp < 500)&&(secInp > 1))){ // if statement to check parameters met
for(i = 0; i < secInp; i++){
rollDice = (rand()%firInp) + 1; // added 1 to rand because if the value is ever zero, you will get an error
printf("%d \n", rollDice);
}
flag = 0; // now the flag variable is set to zero, exiting the while loop
}
else{
printf("Sorry, these numbers don't meet the parameters\nPlease enter a number in the right parameters.\n");
}
}
return 0;
}
I want to input the values I obtain from "rollDice" into an array.
for example: If the user enters firInp and secInp as 6, and they get the following values:
1
2
2
3
1
6
I want these numbers to be stored in an array like so:
arrayA = [1,2,2,3,1,6]
Well it really depends on the particular codebase - my example uses stack frame allocation but it could easily be done using the malloc/calloc method suggested in the comments
(The following is pseudocode, you wil need to integrate it into your application)
int main() {
int dice_rolls[6];
for(int i = 0; i < 6 i++) {
roll = rollTheDice(); // In your program you have your own method for getting this number - the point still stands
dice_rolls[i] = roll;
}
}
My Code so far:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <time.h>
int main()
{
int i;
int rollDice;
int firInp;
int secInp;
printf("Enter the amount of faces you want your dice to have (MAX=24, MIN=1): ");
scanf("%d", &firInp);
printf("Enter the amount of throws you want(MAX=499, MIN=1): ");
scanf("%d", &secInp);
srand ( time(NULL) );
if (((firInp < 25)&&(firInp > 1))&&((secInp < 500)&&(secInp > 1))){
for(i = 0; i < secInp; i++){
rollDice = (rand()%firInp) + 1;
printf("%d \n", rollDice);
}
}
else{
printf("Sorry, these numbers don't meet the parameters. Please enter a number in the right parameters.");
}
return 0;
}
I want to have percentages in my code. I think the way to do that is to first enter the output of my code into an array. If there's another way please feel free to let me know.
edit: I want the output to be something like this:
1 3 4 4 4 5
occurrence of 1: 16.6 percent
occurrence of 3: ..and so on
Instead of entering the output of the random function in an array you could just use that array as a counter, and incrementing the array at position rollDice every time a number appears. Than you could easily extract the percentage by summing all the elements of the array and by dividing each element by that sum.
You can create an array of integers, with size equals to the numbers of possible values your dice can output. Then you use it as a counter for the number of occurences you get, the index of the array will represent that output value (you can use rollDice-1 since 0 isn't a possible output of the dice), and the value at the index will be the number of occurences.
After you finish rolling the dice you just have to print the percentage like this:
for (int i=0;i<firInp;i++) { // firInp: n_faces = n_possible_values
printf("Occurrence of %d: %.1f percent\n", i+1, ((float)array[i]*100)/(float)secInp);
}
Problem Statement
You are given an array of integers of size . You need to print the sum of the elements of the array.
Note: A signed 32-bit integer value uses bit to represent the sign of the number and remaining 31 bits to represent the magnitude. The range of the 32-bit integer is . When we add several integer values, the resulting sum might exceed this range. You might need to uselong long int in C/C++ or long data type in Java to store such sums.
Input Format
The first line of the input consists of an integer. The next lines contain space separated integers describing the array.
Constraints
Output Format
Output a single value equal to the sum of the elements of the array.
Sample Input
5
1000000001 1000000002 1000000003 1000000004 1000000005`
Sample Output
5000000015
My program
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,i;
scanf("%d",&n);
long int a[5],sum;
for(i=0;i<=n-1;i++){
scanf("%ld %ld %ld %ld %ld",&a[1],&a[2],&a[3],&a[4],&a[5]);
}
for(i=0;i<=n-1;i++){
sum = sum + a[i];
}
printf("%ld",sum);
return 0;
}
Error description
Input (stdin):
5
1000000001 1000000002 1000000003 1000000004 1000000005
Your Output (stdout):
140692151765426
Expected Output:
5000000015
Compiler Message:
Wrong Answer
This works fine :
#include <inttypes.h>
int main()
{
int n,i;
scanf("%d",&n);
unsigned long long int a[5];
unsigned long long int sum=0;
for(i=0;i<n;i++)
{
scanf("%llu",&a[i]);
}
for(i=0;i<n;i++)
{
printf("%llu\n",sum);
sum = sum + a[i];
}
printf("\nSum is : %llu",sum);
return 0;
}
Use the ll long-long modifier with the u (unsigned) conversion
You don't basically need an array here. Just
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
long long int a, sum = 0;
while (n--) {
scanf("%lld", &a);
sum += a;
}
printf("%lld", sum);
return 0;
}
The program has following issues:
Array indexing is wrong : Array is of size 5 starting from 0 , you can use only till a[4] , but for scanf() , it tries to read value as &a[5].
No use of for loop when you are using hard coded index a[1],a[2],etc. Instead the below code will be better to get input as follows:
for (i = 0; i < n; i++ ) {
scanf("%ld",&a[i]);
}
Sum includes : sum = sum + a[i];
when i = 0 --> a[0] will have have junk values because input was not taken from user as scanf started from a[1] . Since array is not initialized this is uninitialized auto variable, it might have junk values which will get added in the sum .
sum itself is also not initialized, so it will contain junk value and for very first addition : sum = sum + a[0]; thsi junk value will get added .
Hope this answer your query for unexpected output.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int n,i;
cin >> n;
long long int s=0;
long int a[10];
if(n>=1 && n<=10)
{
for(i=0;i<n;i++)
{
cin>>a[i];
s=s+a[i];
}
}
cout<<s;
return 0;
}
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,i;
scanf("%d",&n);
long int a[10];
long long sum=0;
for(i=0;i<=n-1;i++){
scanf("%ld",&a[i]);
}
for(i=0;i<=n-1;i++){
sum = sum + a[i];
}
printf("%ld",sum);
return 0;
}
I know this is going to be something of a silly slip or oversight on my behalf, but I can't get the array in this to print out correctly. When I run the code and put in my inputs, I get seemingly random numbers.
For example,
number of rooms was 1
wattage of lights was 2
hours used was 2
TV/computers was 2
The output I got was 3930804. What did I miss?
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
int room[20] = {0.0};
int i;
int rooms = 0;
char option = 0;
int lights[20];
int hrsUsed[20];
int Telly_Computer[20];
printf("Enter number of rooms");
scanf_s("%d", &rooms);
for(i=0;i<rooms;i++)
{
printf("input wattage of lights");
scanf_s("%d", (lights+i));
printf("input number of hours use/day (average)");
scanf_s("%d", (hrsUsed+i));
printf("input number of TV/Computers");
scanf_s("%d", (Telly_Computer+i));
}
printf("%d \n", lights);
}
printf("%d \n", lights);
You're printing the array directly. You need to loop over it and print the elements one at a time.
int i;
for (i = 0; i < 20; ++i)
printf("%d\n", lights[i]);
You are just printing the address of lights (and using UndefinedBehavior by the way, address must be printed with %p). You must use a loop to print out all of the contents of each array slot.
for(int i=0;i<(sizeof(lights)/sizeof(int));i++)
printf("%d\n",lights[i]);