How to repeat a for loop in C language - c++

I am a C language beginner. I got this assignment to program for diving score. the rule is that the score has to be between 0 to 10, if the score is invalid, the program should ask for a new score, and there should be at least 4 judges to give the score.
I am having problem with the part with the for loop, I want the program to keep checking if the score is invalid or not, but I couldn't repeat the for loop. please help me, here is my code.
for (index = 0; index < judges; index++)
{
printf ("Enter the score for judges #%d(1-10): ", index + 1);
scanf ("%f", &score[index]);
if ((score[index] >= 0) && (score[index] <= 10))
{
totalscore += score[index];
}
else
{
totalscore = 0;
for (index = 0; index < judges; index++)
{
printf ("11111Enter the score for Judges #%d(0-10): ", index + 1);
scanf ("%f", &score[index]);
if ((score[index] >= 0) && (score[index] <= 10))
{
totalscore += score[index];
return EXIT_SUCCESS;
}

Use this pattern:
do
{
prompt();
scanf(&variable);
}
while (is_invalid (variable));
This way, you only need to check if the variable is valid in one place and you only need to prompt in one place.
Your multiple whiles and ifs are error prone and hard to get right.
So instead of:
printf ("Enter the number of judges (must bewteen 4-8): ");
scanf ("%d", &judges);
if ((judges < 4) || (judges > 8)){
while (true) {
printf ("\ninvaild number of judges\n\nEnter the number of judges (must between 4-8): ");
scanf ("%d", &judges);
if ((judges >= 4) && (judges <= 8)) {
break;
}}}
Use:
do
{
printf ("\n\nEnter the number of judges (must between 4-8): ");
scanf ("%d", &judges);
}
while ((judges < 4) || (judges > 8));

why you use the same variable "index" in the inner for loop? it'll mess up everything. suppose you have 4 judges, you have inputed 3 and 5. you try to input 17, and now the index is 2, right? but the score is invald, so it will goto the "else { xxxxx }" , here you have a inner for loop "for (index = 0; index < judges; index++)" , so the variable "index" come back to 0! IMO in this case, "while" is better:
while ((score[index] < 0) || (score[index] > 10))
printf(xxxxxxx)
scanf(xxxxxxx)
if you really need more than one for loop, you should use different variables:
for (i=0; i<judges; i++)
/*DO STH*/
for (j=0; j<judges; j++)
/*DO STH*/
for (k=0; k<judges; k++)
/*DO STH*/

Looking at your code, I believe that your problem isn't understanding "for" loops in C but rather building the correct flow of your program (your "algorithm" if you will) before approaching the code.
Practice Pseudo-Code for this:
For Each Judge:
Continue to ask for score until it is valid
Or you can break it down even more:
Beginning of loop:
Current Judge = 0
current Score = invalid
Do this while score is invalid
ask score of current judge.
Increment Current Judge
Unless no more judges, go to beginning of loop

Related

I want to know the error in my code. This is to print sum of all even numbers till 1 to N

#include<iostream>
using namespace std;
int main(){
int i = 1;
int sum;
int N;
cout << "Enter a number N: ";
cin >> N;
while(i<=N)
{
if(i%2 == 0)
{
sum = sum + i;
}
else
{
i = i + 1;
}
}
cout << sum;
}
This is to print the sum of all even numbers till 1 to N.
As I try to run the code, I am being asked the value of N but nothing is being printed ahead.
For starters the variable sum is not initialized.
Secondly you need to increase the variable i also when it is an even number. So the loop should look at least like
while(i<=N)
{
if(i%2 == 0)
{
sum = sum + i;
}
i = i + 1;
}
In general it is always better to declare variables in minimum scopes where they are used.
So instead of the while loop it is better to use a for loop as for example
for ( int i = 1; i++ < N; ++i )
{
if ( i % 2 == 0 ) sum += i;
}
while(i<=N)
{
if(i%2 == 0)
{
sum = sum + i;
}
else
{
i = i + 1;
}
}
Let's step through this. Imagine we're on the loop where i = 2 and you've entered N = 5. In that case...
while(i <= N)
2 <= 5 is true, so we loop
if(i%2 == 0)
2 % 2 == 0 is true, so we enter this branch
sum = sum + i;
Update sum, then head back to the top of the loop
while(i <= N)
Neither i nor N have changed, so 2 <= 5 is still true. We still loop
if(i%2 == 0)
2 % 2 == 0 is still true, so we enter this branch again...
Do you see what's happening here? Since neither i nor N are updated, you'll continue entering the same branch and looping indefinitely. Can you think of a way to prevent this? What would need to change?
Also note that int sum; means that sum will have a garbage value (it's uninitialized). If you want it to start at 0, you'll need to change that to
int sum = 0;
You're looping infinitly when i is even because you don't increase it.
Better option would be this if you want to use that while loop :
while(i<=N)
{
if(i%2 == 0)
sum = sum + i;
i=i+1;
}
cout << sum;
If you don't need to do anything when the condition is false, just don't use an else.
No loops are necessary and sum can be evaluated at compile time if needed too
// use unsigned, the whole excercise is pointless for negative numbers
// use const parameter, is not intended to be changed
// constexpr is not needed, but allows for compile time evaluation (constexpr all the things)
// return type can be automatically deduced
constexpr auto sum_of_even_numbers_smaller_then(const unsigned int n)
{
unsigned int m = (n / 2);
return m * (m + 1);
}
int main()
{
// compile time checking of the function
static_assert(sum_of_even_numbers_smaller_then(0) == 0);
static_assert(sum_of_even_numbers_smaller_then(1) == 0);
static_assert(sum_of_even_numbers_smaller_then(2) == 2);
static_assert(sum_of_even_numbers_smaller_then(3) == 2);
static_assert(sum_of_even_numbers_smaller_then(7) == 12);
static_assert(sum_of_even_numbers_smaller_then(8) == 20);
return 0;
}
int main(){
int input; //stores the user entered number
int sum=0; //stroes the sum of all even numbers
repeat:
cout<<"Please enter any integer bigger than one: ";
cin>>input;
if(input<1) //this check the number to be bigger than one means must be positive integer.
goto repeat; // if the user enter the number less than one it is repeating the entry.
for(int i=input; i>0; i--){ // find all even number from your number till one and than totals it.
if(i%2==0){
sum=sum+i;
int j=0;
j=j+1;
cout<<"Number is: "<<i<<endl;
}
}
cout<<endl<<"The sum of all even numbers is: "<<sum<<endl;}
Copy this C++ code and run it, it will solve your problem.
There are 2 problems with your program.
Mistake 1
The variable sum has not been initialized. This means that it has(holds) an indeterminate value. And using this uninitialized variable like you did when you wrote sum = sum + i; is undefined behavior.
Undefined behavior means anything1 can happen including but not limited to the program giving your expected output. But never rely on the output of a program that has undefined behavior.
This is why it is advised that:
always initialize built in types in local/block scope.
Mistake 2
The second problem is that you're not updating the value of variable i.
Solution
You can solve these problems as shown below:
int main(){
int i = 1;
int sum = 0; //INITIALIZE variable sum to 0
int N;
cout << "Enter a number N: ";
cin >> N;
while(i<=N)
{
if(i%2 == 0)
{
sum = sum + i;
}
i = i + 1; //update(increase i)
}
cout << sum;
}
1For more reading(technical definition of) on undefined behavior you can refer to undefined behavior's documentation which mentions that: there are no restrictions on the behavior of the program.

C++ Largest number in array. Positive and negative

I have a task to print maximum int of matrix second line.
Example input:
3 2 (n, m)
-1 -2 <- 1 line
4 5 <- 2 line
2 6 <- 3 line
Max int in second line is 5. My program prints it. But if second line would be -100 -150, it not works. Sure it is because I have max = 0, but I don't know how to use it properly. I'm a student. Thanks in advance.
It is my code:
#include <iostream>
using namespace std;
int main() {
int n, m, max = 0;
cin >> n >> m;
int matrix[10][10];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> matrix[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (matrix[1][j] > max) {
max = matrix[1][j];
}
}
}
if (max == 0 || n == 1) {
cout << "No";
} else {
cout << max;
}
}
And code works pretty good, unless there are negative numbers in second line
You are correct to suspect max = 0;. Why is that a problem? Well, first, perhaps you should try to explain to your rubber duck why it is correct. As you try to do so, you are likely to express an intent along the lines of "this value will not make it through the checks" or "this value will be replaced in the first iteration of the loop". Why? "Because matrix[1][j] > max will be true, so... Hold on, wasn't the problem when matrix[1][j] > 0 is false? So when max is 0, um... problem?"
The overall strategy is valid, but there is a requirement that max be initialized to a low enough value. There are two common strategies I can think of at the moment.
Use a value that is as low as possible for the type you are using. That is:
int max = std::numeric_limits<int>::lowest();
Use the value from the first iteration of the loop. No need to provide a value that is just going to be replaced anyway. There are some caveats for this, though. The most relevant for your example can be expressed as a question: what if there is no first iteration? (Perhaps there is only one row? Perhaps there are no columns?) Also, you would need to initialize max between your loops, after the matrix has been given values.
int max = (n > 1 && m > 0) ? matrix[1][0] : /* what value do you want here? */;

How to improve speed for time limit exceeded

Is there any way to improve the running time for this program? I get time limit exceed error from the online judge, and it seems like my program is running slow?
So, this is the question for this program: http://www.spoj.com/problems/PRIME1/
My code (language c):
#include <stdio.h>
void FindPrime (int m, int n)
{
int i, prime = 1;
if (m <= n)
{
for (i = m - 1; i > 1; i--)
{
if (m % i == 0)
{
prime = 0;
break;
}
}
if (prime == 1 && m != 1)
printf ("%d\n", m);
FindPrime (m + 1, n);
}
}
int main ()
{
int num1, num2, i, cases;
scanf ("%d", &cases);
while (cases != 0)
{
scanf ("%d %d", &num1, &num2);
FindPrime (num1, num2);
printf ("\n");
cases--;
}
return 0;
}
To solve this question you need to learn "Sieve of Eratosthenes".
First, get the idea of how it works from here. But, this is not enough to solve this question. Since, the complexity of the algorithm is O(n.log(log(n))). Therefore, if we put n = 1000000000. It will surely fail to execute.
Now, time to optimize it. Read it from here. But, we are done yet.
(Please read this section after you are done with the above two) Since we are to find the prime numbers is the range [m,n]. So, first create a list of prime numbers (let's call it primeList) between the range of 2 to 32000 using sieve of Eratosthenes (sqrt(10^9) = 31622.7, which is less then 32000). Now, check for every number k in the range of [m,n]
3.1. If the number k is in the range of 2-32000, and the number is in primeList. Print it.
3.2. If the number k > 32000, and is not divisible by all the numbers which are <= sqrt(k) and also in primeList. Print it. Else, ignore or don't print it. ( mind it '1' is not prime number).
You may check my solution. But, it is implemented slightly different than i explained although the concept applied is same.
Your code it calculating/listing the prime numbers between to numbers m & n cases number of times. Now you algorithm takes O(n) time for each value of m & hence total time taken by your code for a single case is O(n x m) or O(n^2) times..So in total the time taken is cases * O(n^2).. This is bad..Use some better algorithm to ease the complexity..
Check this modified version of Sieve of Eratosthenes
#include <stdio.h>
#define SIZE (int)(sizeof(boolean) / sizeof(int))
int main(){
int up,low;
int i,j;
printf("\nEnter the upper limit: ");
scanf("%d", &up);
printf("\nEnter the lower limit: ");
scanf("%d", &low);
int boolean[up];
int list[up];
for(i = 0; i < SIZE; i++){
boolean[i] = 1;
list[i] = 2 + i;
}
for(i = 0; i <= up; i++){
if (boolean[i] == 1)
for(j = i +1; j < SIZE; j++)
if(list[j] % list[i] == 0)
boolean[j] = 0;
}
for(i = 0; i < SIZE; i++)
if(boolean[i] == 1 && list[i] >= low && list[i] <= up)
printf("%d ", list[i]);
printf("\n");
return 0;
}
This will not visit one number more than once & hence will reduce the complexity in logarithmic scale.

Loop doesn't stop!? (C++ programming)

I don't understand why my loop stops, i want the user to keep asking for a number and find its square root. If the user enters negative number THEN it should stop. please help, cant see my mixtake...
CODE:
int main (void)
{
double number, calc;
printf ("Enter a number to find its sqrt");
while (1)
{
scanf ("%lf",&number);
if (number > 0)
{
calc = sqrt(number);
printf ("The sqrt of %lf is %lf", &number, &calc);
}
else
printf ("Try again:\n");
}
return (0);
}
Not allowed to answer but, I see thanks you two!!
(Didnt know i must exist loop)
You must exit the loop somewhere.
if(number < 0)
break;
Alternatively you can do this (which would be better in this case):
number = 0;
while(number >= 0)
{
....
}

First Semester CS Student needs help understanding statement in While loop

I normally post on DreamInCode.net but the site seems to be down right now. I'm a first semester CS student at De Anza. I don't understand really what the lineCount = 1; does in the else statement. I know what it does when I remove the statement but I don't understand it. If I could have someone explain it to me maybe in a different way that the book just happens to skip over, I would greatly appreciate it.
#include <stdio.h>
int main (void) {
int num;
int lineCount;
printf ("\nEnter a starting number to decend between 1 and 100: ");
scanf ("%d", &num);
if (num > 100)
num = 100;
lineCount = 0;
while (num >= 0)
{
if (lineCount < 10)
lineCount++;
else
{
printf ("\n");
lineCount = 1; // this line here is what I don't understand
}
printf ("%4d", num--);
}
return 0;
}
lineCount isn't actually counting lines. It's counting the number of numbers you've printed on the current line.
When that reaches 10, it breaks the line and starts a new one, resetting the counter to 1. 1 instead of 0 because you're placing another number on the new line.