C - how to fix to work printf in this code? - c

I'm C begginer.I'm making typing practice program for practice.
In line 42,it doesn't work printf.I want print rand_n.
I think it maybe array problem but i can't fix this code.
can you help me?
Thanks.Have a good day!
1 #include <stdio.h>
2 #include <time.h>
3 #include <string.h>
4 #include "getch.h"
5
6 int main()
7 {
8 char se[5][6][100]={{"AND THEN THERE WERE NONE", ....
27 ..... ," Young Lord L had surrendered to Cupid at last"}};
28
29
30 char mysent;
31 int accu=0,pro=0;
32 int rand_n;
33 double typing=0.0;
34 srand(time(NULL));
35 rand_n=rand()%1000;
36 time_t start=0,end=0;
37 typing = accu*60.00/(end-start);
38
39
40 printf(">> typing practice <<\n");
41 printf("accuracy : %d%% typing_pre_sec : %d\n",accu,typing);
42 printf("%s\n",se[rand_n]);

se is a 3D array of character. Or a 2D array of strings. You are indexing it only once, so se[rand_n] is actually an array of strings. You probably don't want it to be a 3D array in the first place. Remove [5] from the declaration.
Also, rand_n can be anywhere between 0 and 999. You probably want to do rand() % 5 or something.

Related

C program to check lottery numbers: why some tests fail?

This program takes as an input the following lines:
23 12 33 19 10 8
5
23 19 8 12 60 18
14 60 12 44 54 10
8 3 12 19 33 10
33 15 7 60 12 10
22 12 19 23 33 11
23 12 33 19 10 8 ( The first line ) are the lottery results.
n ( in this specific case, 5 ) informs how many lines will follow below.
Each line has 6 numbers. The number order doesn't matter.
The rules are: numbers range from 1 to 60 ( including 1 and 60 ) and they never repeat themselves in the same line.
The variable "quadra" stores how many lines have got 4 numbers right.
The variable "quina" stores how many lines have got 5 numbers right.
The variable "sena" stores how many lines have got 6 numbers right.
So, a computer program is running some tests over my code below and it's claiming that it goes wrong for most of them, but I can't see what's the problem here. Does anybody have a clue? Is this code wrong, or is there something wrong with the software that's testing this code?
#include <stdio.h>
int main(){
int mega[6];
int v[50500][6];
int n,swap;
int i,j,k; //counters
int quadra,quina,sena;
quadra = 0;
quina = 0;
sena = 0;
for(i=0;i<6;++i) scanf("%i",&mega[i]); //first line, lottery results
scanf("%i",&n);
for(i=0;i<n;++i){
for(j=0;j<6;++j){
scanf("%i",&v[i][j]);
}
}
for(i=0;i<n;++i){
for(j=0;j<6;++j){
for(k=0;k<6;++k){
if(v[i][j] == mega[k]){
v[i][j] = 61;
}
}
}
}
//reverse bubble sort
for(i=0;i<n;++i){
for(j=0;j<6;++j){
for(k=j+1;k<6;++k){
if(v[i][j] < v[i][k]){
swap = v[i][k];
v[i][k] = v[i][j];
v[i][j] = swap;
}
}
}
}
for(i=0;i<n;++i){
for(j=0;v[i][j] == 61 && j<6;++j);
if(j == 4) ++quadra;
else if(j == 5) ++quina;
else if(j == 6) ++sena;
}
return 0;
}
Your code is true, I understood and tried the flow of it. Looks fine but if you dont need to sort everyline (and use j as a counter in this loop for(j=0;v[i][j] == 61 && j<6;++j); ), you can use simpler ifstatements to compare real lottery results with the ones that entered. What I mean is that your algorithm is a little complex. Try a simple one and see how it works.
Yes, there are a couple of noteworthy issues with your code:
Compile time indicates possibility of uninitialized variable:
But, run-time results in fatal run-time at unknown source location. Stack overflow. It is likely due to this line:
int v[50500][6];
Increase your stack size. It needs to be about 2.5Mbytes for v alone.
Also, this line may not be what you intended:
for(i=0;i<6;++i) scanf("%i",&mega[i]); //first line, lottery results
^
If you meant to loop around the remainder of the code, remove the ; after the for() statement, and use curly braces:
for(i=0;i<6;++i) scanf("%i",&mega[i]) //first line, lottery results
{
scanf("%i",&n);
....

I am trying to set a value to an array, but I can't seem to figure out why what I did on line 31 is wrong

I have a data file in the format <0:00> - <19321> , <1:00> - <19324>, up to <24:00> - <19648>, so for every hour there is the total power used so far(the total is incremented), I am supposed to calculate the power used, find the average, and the highest usage of power and its index(time), (I don't need help with finding the max power used at its time index). I traced the problem down to line 31, but I don't understand why what I did was wrong. Can someone explain to me why the code in line 31 isn't saving the value of power used into the array? And how I can fix it? Thanks in advance!
float compute_usage(int num, int vals[], int use[], int *hi_idx)
15 {
16 int i;// i is a general counter for all for loops
17 int r1, r2, u, v, pow_dif, temp;//for loop 1
18 int tot;//for loop 2
19 int max_use, init, fina, diff;//for loop 3 //don't have to worry about this for loop, I am good here
20 float avg;//average power used
21
22 for(r1=r2=i=u=v=0;i<num;i++)//for loop 1
23 {
24 r1= vals[v++];//I set values of every hour as reading 1 & 2(later)
25 #ifdef DEBUG
26 printf("pre-debug: use is %d\n", use[u]);
27 #endif
28 if(r1!=0 && r2!=0)
29 {
30 pow_dif = (r1 - r2);//I take the to readings, and calculate the difference, that difference is the power used in the interval between a time period
31 use[u++] = pow_dif; //I'm suppose to save the power used in the interval in an array here
32 }
33 r2=r1;//the first reading becomes the second after the if statement, this way I always have 2 readings to calculate the power used int the interval
34 #ifdef DEBUG
35 printf("for1-debug3: pow_dif is %d\n", pow_dif);
36 printf("for1-debug4: (%d,%d) \n", u, use[u]);
37 #endif
38
39 }
40 for(tot=i=u=0;i<num;i++)//for loop 2
41 {tot = tot + use[u++];}
42
43 avg = tot/(num-1);
44 #ifdef DEBUG
45 printf("for2-debug1: the tot is %d\n", tot);
46 printf("for2-debug2: avg power usage is %f\n", avg);
47 #endif
Just to understand, how did you figure out that the code in line 31 is problematic? Is it the printf statement in line 36?
When you do this:
use[u++] = pow_dif; //I'm suppose to save the power used in the interval in an array here
printf("for1-debug4: (%d,%d) \n", u, use[u]);
The "u" variable in printf statement is incremented in the previous operation (u++), so you are looking past the element you changed.
use[u++] = pow_dif; //I.e. u=0 here, but u=1 after this is executed.
printf("...\n", u=1, use[1]);
What is the "i" for in this loop? Why don't you try "u++" in the for statement instead of "i++" and remove the "u++" in the use assignment expression?

How SubString,Limit Using C? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Section#1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
int main(int argc, char **argv)
{
static const unsigned char text[] = "000ßh123456789";
int32_t current=1;
int32_t text_len = strlen(text)-1;
/////////////////////////////////
printf("Result : %s\n",text);
/////////////////////////////////
printf("Lenght : %d\n",text_len);
/////////////////////////////////
printf("Index0 : %c\n",text[0]);
printf("Index1 : %c\n",text[1]);
printf("Index2 : %c\n",text[2]);
printf("Index3 : %c\n",text[3]);//==> why show this `�`?
printf("Index4 : %c\n",text[4]);//==> why show this `�`?
printf("Index0 : %c\n",text[5]);
/////////////////////////////////
return 0;
}
why text[3] and text[4] show �?
how can also support utf-8 character in Index?
Section#2
I want write a function like mb_substr in php.
(verybigstring or string) mb_substr ( (verybigstring or string) input , (verybigint or int) start [, (verybigint or int) $length = NULL ] )
Some Example:
mb_substr("hello world",0);
==>hello world
mb_substr("hello world",1);
==>ello world
mb_substr_two("hello world",1,3);
==>el
mb_substr("hello world",-3);
==>rld
mb_substr_two("hello world",-3,2);
==>rldhe
My Question is Section#1
Can anyone help me?(please)
The Unicode character set currently includes more than 128,000 characters (which I shall henceforth call Code Points to avoid confusion) with space reserved for far, far more. As such, a char which is only 8 bits in size on modern general-computing machines can't be used to contain a Code Point.
UTF-8 is a way of encoding these Code Points into bytes. The following are the bytes you placed in text[] (assuming UTF-8 was used to encode the Code Points) and what they represent:
i: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
text[i]: 0x30 30 30 C3 9F 68 31 32 33 34 35 36 37 38 39 00
-- -- -- ----- -- -- -- -- -- -- -- -- -- -- --
Code Point: U+30 30 30 DF 68 31 32 33 34 35 36 37 38 39 0
Graph: 0 0 0 ß h 1 2 3 4 5 6 7 8 9
As you can see, UTF-8 is a variable-width encoding. A single Code Points encodes to a variable number of bytes. This means you can't translate indexes-into-text into indexes-into-array-of-bytes without scanning the array.
A Code Point encoded using UTF-8 starts with
0b0xxxxxxx Represents an entire Code Point
0b110xxxxx The start of a 2-byte sequence
0b1110xxxx The start of a 3-byte sequence
0b11110xxx The start of a 4-byte sequence
The only other form of bytes you will encounter in UTF-8 is
0b10xxxxxx A continuation byte (the 2nd, 3rd or 4th byte of sequence)
A simple way to find the nth Code Point in a string (if you assume the input is valid UTF-8) is to search for the nth char for which (ch & 0xC0) != 0xC0 is true. You can use the same approach to count the number of Code Points in a string.

Pointers and dynamic memory allocation in c

im reading lines from an input text file and i'm storing the lines in an array array1
once im done reading id like to print out the elements in the array.. id like to do dynamic memory allocation.. but thats after i get this part working..
my code so far:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 256
int main(){
FILE* fp;
fp=fopen("input.txt","r");
char currentline[MAX_LEN];
int i=0;
char *array1;
array1 = malloc(10*MAX_LEN);
while(fgets(currentline,MAX_LEN,fp)!=NULL && i<10){
strcpy((char *)&array1[i],currentline);
printf("%s\n",currentline);
printf("array1= %s\n",(&array1[i]));
i++;
}
for (;i>=0;i--){
printf("%s\n",(&array1[i]));
}
}
input1.txt
101
84
65
13
hello
90
24
94
73
70
68
94
65
output:
101
array1= 101
84
array1= 84
65
array1= 65
13
array1= 13
hello
array1= hello
90
array1= 90
24
array1= 24
94
array1= 94
73
array1= 73
70
array1= 70
0
70
770
9770
29770
929770
h929770
1h929770
61h929770
861h929770
1861h929770
what i want it to do is print out each element..but its printing the first character of each line.. whats causing this?
In this line:
strcpy((char *)&array1[i],currentline);
you copy currentline to the to the array1, starting at its i-th element.
So basically you first copy currentLine starting at array[0], then leave first element and copy next currentLine starting at array[1] and so on. Every time you overwrite previous currentLine except its first element.
What you want (I suppose) to do is:
strcpy(&array1[i*MAX_LEN],currentline);
which will save every currentLine in its own block of 256 characters.
For i=0 you will write starting at array[0],
for i=1 at array[256]...
Also drop those ugly (char *) casts, they are unnecessary in here.
You should check if malloc worked before dereferencing array1, too

How do you read in a number and a string from a file in C? Keeping in mind that some lines in the file only has a number and not a string

I have a file with multiple lines, and on each line there is a number followed by a space and then a string. But some lines in the file do not have a string following the digit. Here is part of the file:
10
17
38 So You Want to Be a Rock 'N' Roll Star
22 Have You Seen Her Face
12 C.T.A. - 102
16 Renaissance Fair
12 Time Between
23 Everybody's Been Burned
18 Thoughts and Words
12 Mind Gardens
13 My Back Pages
21 The Girl with No Name
3 Why
19 It Happens Each Day
16 Don't Make Waves
13 My Back Pages
12 Mind Gardens
11 Lady Friend
18 Old John Robertson
19
13 Ice Cream Man
14 Hang on Sloopy
Here is what i have so far:
#include <stdio.h>
typedef struct
{
int num_tracks;
char tracks[];
}album_store;
int main(int argc, char *argv[])
{
album_store album[10000];
int numb_tracks;
char line;
int i=0;
if (argc <2 )
{
printf("You need at least one argument\n");
}
else
{
FILE *file;
file=fopen(argv[1],"r");
while(fscanf(file,"%d %[^\n]",&numb_tracks,album[i].tracks) != EOF)
{
album[i].num_tracks=numb_tracks;
printf("%d %s\n",album[i].num_tracks,album[i].tracks);
i++;
}
}
}
Now my code reads in the lines, but not the spaces. Or rather it does not know how to detect if a line does not have a string after the digit. The output of my code looks like:
10 17
38 So You Want to Be a Rock 'N' Roll Star
22 Have You Seen Her Face
12 C.T.A. - 102
16 Renaissance Fair
12 Time Between
23 Everybody's Been Burned
18 Thoughts and Words
12 Mind Gardens
13 My Back Pages
21 The Girl with No Name
3 Why
19 It Happens Each Day
16 Don't Make Waves
13 My Back Pages
12 Mind Gardens
11 Lady Friend
18 Old John Robertson
19 13 Ice Cream Man
My question is how do i get the output of my code to match the input from the file? What would i have to change in my code? Any help is greatly appreciated!
You can't (reasonably) use fscanf() unless you know that all your lines are exactly the same correct format. Instead, I would suggest using fgets() to get one line of text at a time, then parse that line (perhaps using sscanf()).
For example,
char buf[1000];
while (fgets(buf, sizeof(buf), file) != NULL) {
sscanf(buf,"%d %[^\n]",&numb_tracks,album[i].tracks);
// etc
}
You'll want to add checking of the return value of sscanf() to the above sample.

Resources