Calculate the average score - c

Why my calculate the average score is wrong?
I have a function:
int student_average_scope() {
char name[50];
int group;
int exam;
int average = 0;
int digit = 0;
int counter_digits = 0;
for (int i = 0; i < 4; i++) {
sscanf(student_list[i], "%d %[^0-9] %d", &group, name, &exam);
while (exam > 0) {
digit = exam % 10;
average += digit;
counter_digits++;
exam = exam / 10;
}
printf("%.1f\n", (double)average / counter_digits);
}
return 0;
}
Where student_list[i] = "4273 Константинопольский А. А. 4333 знзнз" average is equal 3.9, but right answer 3.2! And if I make simple function, calculate the average score give me right output (3.2). Where I made mistake?
int student_average_scope() {
int exam = "4333";
int average = 0;
int digit = 0;
int counter_digits = 0;
while (exam > 0) {
digit = exam % 10;
average += digit;
counter_digits++;
exam = exam / 10;
}
printf ("%.1f\n", (double) average / counter_digits);
return 0;
}

The issue is that you are failing to reset the variables back to zero when you move from one record to the next in your for loop. What you should do is this:
for (int i = 0; i < 4; i++) {
average = 0;
counter_digits = 0;
...

Related

Gauss-seidel method algorithm code giving inf numbers in solution

I'm not sure why I'm getting inf values when plugging in a 3 equation answer, another set of eyes on the equation loop would be useful, when I use a single equation the number of iterations is correct but when it's more than that the numbers multiply to inf.
Inputs for the code I used
Enter the Total Number of Equations: 3
Enter Allowed Error: 0.5
Enter the Co-Efficients
Matrix[1][1] = 1
Matrix[1][2] = 2
Matrix[1][3] = 3
Matrix[1][4] = 4
Matrix[2][1] = 2
Matrix[2][2] = 3
Matrix[2][3] = 4
Matrix[2][4] = 5
Matrix[3][1] = 3
Matrix[3][2] = 4
Matrix[3][3] = 5
Matrix[3][4] = 6
#include<stdio.h>
#include<math.h>
int main()
{
int count, t, limit;
float temp, error, a, sum = 0;
float matrix[10][10], y[10], allowed_error;
printf("\nEnter the Total Number of Equations:\t");
scanf("%d", &limit);
printf("Enter Allowed Error:\t");
scanf("%f", &allowed_error);
printf("\nEnter the Co-Efficients\n");
for(count = 1; count <= limit; count++)
{
for(t = 1; t <= limit + 1; t++)
{
printf("Matrix[%d][%d] = ", count, t);
scanf("%f", &matrix[count][t]);
}
}
for(count = 1; count <= limit; count++)
{
y[count] = 0;
}
do
{
a = 0;
for(count = 1; count <= limit; count++)
{
sum = 0;
for(t = 1; t <= limit; t++)
{
if(t != count)
{
sum = sum + matrix[count][t] * y[t];
}
}
temp = (matrix[count][limit + 1] - sum) / matrix[count][count];
error = fabs(y[count] - temp);
if(error > a)
{
a = error;
}
y[count] = temp;
printf("\nY[%d]=\t%f", count, y[count]);
}
printf("\n");
}
while(a >= allowed_error);
printf("\n\nSolution\n\n");
for(count = 1; count <= limit; count++)
{
printf("\nY[%d]:\t%f", count, y[count]);
}
return 0;
}
Your code is correct. The issue is that Gauss-Seidel method does not always converge. The convergence criteria is that the matrix A must be either:
symmetric positive-definite
strictly or irreducibly diagonally dominant
The input matrix you used is neither symmetric, nor diagonally-dominant. Hence, the method fails to converge to a solution.

Luhn's Checksum Algorithm 's Solution in C

here's the sample of the input and the output
What's wrong with my code? The output on my Dev-C++ is alright, no problem, but when I submitted it to an online judge it turns out as wrong answer. I don't know what's the problem, and I prefer to not change most of them (because this code turns out from my difficult brain, I'm sorry). But any help would be appreciated, thank you.
#include <stdio.h>
int main () {
int T = 0, max = 0, X, sum = 0;
scanf ("%d", &T);
long long int N = 0;
//2891
for (int i = 0; i < T; i++) {
sum = 0;
scanf ("%lld", &N);
int A = N;
while (A / 10 != 0) {
A = A / 10;
max++;
}
int length = max;
for (int j = length; j >= 0; j--) {
int X = N % 10; //1
sum += X;
int Xx = N / 10; //289
int Y = Xx % 10; //9
int Yy = Y * 2; //18
if (Yy > 9) {
Yy -= 9; //9
}
sum += Yy;
N = Xx / 10;
}
if (sum % 10 == 0) {
printf ("PASS\n");
} else printf ("FAIL\n");
}
return 0;
}

C - Middle Square Random Number Generator

So, I tried to implement the Middle Square PRNG method, to generate the first 100 numbers. It works well until a certain point, when I get as a result negative numbers.
I used the time library to change the values on my temp array, so that it won't get stuck on the same sequence, where the number ends with two zeros.
My code :
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
time_t now;
struct tm *tm;
unsigned long int prng(int seed)
{
int num = seed * seed;
int t[10], inc = 0;
//Reverse number in an array
while(num != 0)
{
t[inc] = num%10;
num /= 10;
inc++;
}
int min = inc/4;
int max = inc / 2 + min;
int temp[10];
//Assign the middle square part to another table
for(int i = min; i <= max; i++)
{
temp[i-min] = t[i];
}
for(int i=0; i < max-min; i++)
{
//Check if there is a "0" "0" sequence, if TRUE - replace with current time (seconds)
if(temp[i] == 0 && temp[i+1] == 0)
{
now = time(0);
tm = localtime(&now);
temp[i] = tm->tm_sec/10;
temp[i + 1] = tm->tm_sec%10;
}
}
//Transform the squared array into an integer
unsigned long int k = 0;
for (int i = 0; i <= max-min; i++)
k = 10 * k + temp[i];
return k;
}
int main()
{
unsigned long int n = 123; //seed
printf("%d ", n);
for(int i = 0; i<100; i++)
{
n = prng(n);
printf("\n%d ", n);
}
return 0;
}
The results that I get:
123
215
226
701
419
6557
24992
7064
7099
85930
-696950
8997
6490
10212
94824
36561
760763
-724206
30238
66334
22325
65048
-94273
...

Largest palindrome made from the product of two 3-digit numbers with C

The code is trying to find the largest palindrome made from the product of two 2-digit numbers. The answer is 91*99 = 9009 but I keep getting 990, which is not even a palindrome. I really appreciate the help!
#include <stdio.h>
int main()
{
int i = 10;
int j = 10;
int a = 0;
int b = 0;
int array[100] = {0};
int divider = 10;
int num;
int great;
int product;
int n;
int flag;
/*Loop through first 2 digit number and second 2 digit number*/
while (i<100)
{
while (j < 100)
{
product = i*j;
array [a] = product % 10;
n = product / divider;
while (n != 0)
{
a++;
num = n%10;
divider *=10;
array[a]=num;
n = product/divider;
}
flag = 0;
while (b<a)
{
if (array[b] != array[a])
{
flag = 1;
}
b++;
a--;
}
if (flag == 0)
{
great = product;
}
j++;
a = 0;
b = 0;
}
i++;
}
printf("The largest palindrome is %d \n", great);
return 0;
}
Here is a code snippet you can try.
#include <stdio.h>
void main()
{
int a = 1; // first integer
int b = 1; // second integer
int currentNumber;
int currentPalin; if a palindrome is found, its stored here
while (a<100){ //loop through the first number
while (b<100){ // loop through the second number
currentNumber = a*b;
if (currentNumber == reverse(currentNumber) ){ //check for palindrome
currentPalin = currentNumber;
}
b = b+1; //increment the second number
}
b = a; // you could have set b=1 but it would not be an efficient algorithm because
//some of the multiplication would occur twice. eg- (54*60) and (60*54)
a = a +1; //increment the first number
}
printf ("Largest palindrom is %d \n", currentPalin);
getchar();
}
// method for finding out reverse
int reverse(int n){
int reverse = 0;
while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
// when you divide a number by 10, the
//remainder gives you the last digit. so you are reconstructing the
//digit from the last
n = n/10;
}
return reverse;
}
Update:- As suggested by M Oehm, I have modified the code to make it more general.
#include <stdio.h>
void main()
{
int a = 1;
int b = 1;
int currentNumber;
int currentPalin=0;
while (a<100){
while (b<100){
currentNumber = a*b;
if (currentNumber == reverse(currentNumber) ){
if (currentNumber>currentPalin){
currentPalin = currentNumber;
}
}
b = b+1;
}
b = 1;
a = a +1;
}
if (currentPalin==0){
printf("No Palindrome exits in this range");
}
else {
printf ("Largest palindrome is %d \n", currentPalin);
}
getchar();
}
int reverse(int n){
int reverse = 0;
while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}
return reverse;
}
An alternative approach to solve the problem.
#include<stdio.h>
int reverse(int num)
{
int result = 0;
while( num > 0)
{
result = result * 10 + (num%10);
num/=10;
}
return result;
}
int main()
{
int last_best = 1;
int best_i=1;
int best_j = 1;
const int max_value = 99;
for( int i = max_value ; i > 0 ; --i)
{
for(int j = i ; j > 0 ; --j){
int a = i * j;
if( last_best > a )
break;
else if ( a == reverse(a) )
{
last_best = a;
best_i = i;
best_j = j;
}
}
}
printf("%d and %d = %d\n", best_i,best_j,last_best);
}
And it is quite simple to follow.
It seems that you do not reinitialize variables at the beginning of loop. They keeps values from previous iterations. For example, j and divider. Put
j = 10;
before starting "j" loop, i.e.:
j = 10;
while (j < 100) ...
The same for divider:
...
j = 10;
while (j < 100) {
divider = 10;
...
If you were using for loops you would avoid this problem naturally:
for(i=10; i<100; i++) {
for(j=10; j<100; j++) {
...
}
}

Fill a defined 2D Array with random numbers in C / Average / Max Value

I've created this 2D 21x21 array that has all it's values set to -1. I wrote it to print the address and value and somehow it only starts at [6][19] why?
What i want to do is to replace some of the -1 values with random numbers from 0 to 100 in the same array. I know i need to seed it with srand but i'm having problems connecting the functions since i'm a total beginner in C.
EDIT 1:
Now i can print the whole array and fill it with random numbers. For the -1 values i just assigned directly which for this case its fine.
What i'm trying now is finding the average of all the values and the maximum number, so what i have is:
#include<stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int a[21][21], i , j;
for (i = 0; i < 21; i++)
{
for ( j = 0; j < 21; j++)
{
a[i][j] = GetRand(0, 100);
a[7][15] = -1;
a[10][6] = -1;
a[13][5] = -1;
a[15][17] = -1;
a[17][17] = -1;
a[19][6] = -1;
printf("%3d" , a[i][j]);
}
printf("\n");
}
return 0;
}
// random seed
int GetRand(int min, int max);
int get() {
int i, r;
for (i = 0; i < 21; i++)
{
r = GetRand(0, 100);
printf("Your number is %d \n", r);
}
return(0);
}
int GetRand(int min, int max)
{
static int Init = 0;
int rc;
if (Init == 0)
{
srand(time(NULL));
Init = 1;
}
rc = (rand() % (max - min +1) +min);
return (rc);
}
// average
int avg()
float sum=0.0;
for(i = 0; i <= 21; i = i + 1) {
for(j = 0; j <= 21; j = j + 1){
sum = sum + a[21][21];
}
printf("The the average number is %.2f\n", sum/21);
}
//find maximum of all values
int *pv = &a[0][0];
max = min = 0;
for (i = 1; i < i*j; ++i){
if (pv[i] > pv[max])
max =i;
if (pv[i] < pv[min])
min = i;
}
printf("The max value is %d in row %d, col %d\n", pv[max], max/j, max%j);
return 0;
}
For the average function the compiler tells me that expected a declaration before i, which is "float sum=0.0;" but i haven't been able to fix that yet.
For the finding the max function i'm not sure yet what i'm doing there, i just have a vague idea of how it's done...am i going in the right direction?
Thanks!
It's very simple: Just assign the result of your GetRand function to the matrix entry.

Resources