scanning from file to generate binary tree - c

I am writing a program to generate permutation of a given series of numbers like
10,11,12,32,33,45,56,67
then output (which is all permutation of this series) I am storing in a file result.txt
I have to generate a binary tree from each line of input in result.txt
I know the program to create a binary tree when a user gives numbers as input but here I am having a file as an example I had initially given input as 9 numbers 87,80,65,45,32,22,109,9,6 I am storing in result.txt as follows.
87 80 65 45 32 22 109 9 6
87 65 80 45 32 22 109 9 6
87 65 45 80 32 22 109 9 6
87 65 45 32 80 22 109 9 6
87 65 45 32 22 80 109 9 6
87 65 45 32 22 109 80 9 6
87 65 45 32 22 109 9 80 6
87 65 45 32 22 109 9 6 80
65 87 45 32 22 109 9 6 80
65 45 87 32 22 109 9 6 80
65 45 32 87 22 109 9 6 80
65 45 32 22 87 109 9 6 80
65 45 32 22 109 87 9 6 80
65 45 32 22 109 9 87 6 80
65 45 32 22 109 9 6 87 80
65 45 32 22 109 9 6 80 87
45 65 32 22 109 9 6 80 87
45 32 65 22 109 9 6 80 87
45 32 22 65 109 9 6 80 87
45 32 22 109 65 9 6 80 87
45 32 22 109 9 65 6 80 87
45 32 22 109 9 6 65 80 87
45 32 22 109 9 6 80 65 87
45 32 22 109 9 6 80 87 65
32 45 22 109 9 6 80 87 65
32 22 45 109 9 6 80 87 65
32 22 109 45 9 6 80 87 65
32 22 109 9 45 6 80 87 65
32 22 109 9 6 45 80 87 65
32 22 109 9 6 80 45 87 65
32 22 109 9 6 80 87 45 65
32 22 109 9 6 80 87 65 45
22 32 109 9 6 80 87 65 45
22 109 32 9 6 80 87 65 45
22 109 9 32 6 80 87 65 45
22 109 9 6 32 80 87 65 45
22 109 9 6 80 32 87 65 45
22 109 9 6 80 87 32 65 45
22 109 9 6 80 87 65 32 45
22 109 9 6 80 87 65 45 32
109 22 9 6 80 87 65 45 32
109 9 22 6 80 87 65 45 32
109 9 6 22 80 87 65 45 32
109 9 6 80 22 87 65 45 32
109 9 6 80 87 22 65 45 32
109 9 6 80 87 65 22 45 32
109 9 6 80 87 65 45 22 32
109 9 6 80 87 65 45 32 22
9 109 6 80 87 65 45 32 22
9 6 109 80 87 65 45 32 22
9 6 80 109 87 65 45 32 22
9 6 80 87 109 65 45 32 22
9 6 80 87 65 109 45 32 22
9 6 80 87 65 45 109 32 22
9 6 80 87 65 45 32 109 22
9 6 80 87 65 45 32 22 109
6 9 80 87 65 45 32 22 109
6 80 9 87 65 45 32 22 109
6 80 87 9 65 45 32 22 109
6 80 87 65 9 45 32 22 109
6 80 87 65 45 9 32 22 109
6 80 87 65 45 32 9 22 109
6 80 87 65 45 32 22 9 109
6 80 87 65 45 32 22 109 9
80 6 87 65 45 32 22 109 9
80 87 6 65 45 32 22 109 9
80 87 65 6 45 32 22 109 9
80 87 65 45 6 32 22 109 9
80 87 65 45 32 6 22 109 9
80 87 65 45 32 22 6 109 9
80 87 65 45 32 22 109 6 9
80 87 65 45 32 22 109 9 6
now the problem is if I use a function like fgets()
#include<stdio.h>
#include<stdlib.h>
int main ()
{
FILE *fp;
char line[60];
fp=fopen("result.txt","r");
while (fgets(line,60,fp)){
puts(line);
}
fclose(fp);
}
I just want the array elements to come separately in different array elemnents with output of puts or fgets this is what I am not able to think of,
any idea of doing this as how to approach this to solve is welcome. Since I do not knew how to store the output of permutation generation in array so I thought of storing output in a file. Upto here program is fine but after this how do I read each integer of series from line in result.txt is my problem I am not able to think of any C implemented function as to do so.

Related

C function printing out additional values

I used the below C code in a question which asked to reverse the given array.
But it is outputing some additional values.
Please tell if there's a mistake in code.
Edit : Code passed all test cases after a newline after every test case, but I am still wondering why I was getting those additional values as output. Can any CS major explain.
#include <stdio.h>
int main() {
int t;
int n;
scanf("%d", &t);
while(t>0){
scanf("%d",&n);
int arr[n];
for(int i = 0; i<n; i++){
scanf("%d", &arr[i]);
}
for(int p = n-1; p>0; p--){
printf("%d ", arr[p]);
}
printf("%d", arr[0]);
t--;
}
return 0;
}
Input:
1
84
86 77 15 93 35 86 92 49 21 62 27 90 59 63 26 40 26 72 36 11 68 67 29 82 30 62 23 67 35 29 2 22 58 69 67 93 56 11 42 29 73 21 19 84 37 98 24 15 70 13 26 91 80 56 73 62 70 96 81 5 25 84 27 36 5 46 29 13 57 24 95 82 45 14 67 34 64 43 50 87 8 76 78 88
Expected Output :
88 78 76 8 87 50 43 64 34 67 14 45 82 95 24 57 13 29 46 5 36 27 84 25 5 81 96 70 62 73 56 80 91 26 13 70 15 24 98 37 84 19 21 73 29 42 11 56 93 67 69 58 22 2 29 35 67 23 62 30 82 29 67 68 11 36 72 26 40 26 63 59 90 27 62 21 49 92 86 35 93 15 77 86
My Output :
88 78 76 8 87 50 43 64 34 67 14 45 82 95 24 57 13 29 46 5 36 27 84 25 5 81 96 70 62 73 56 80 91 26 13 70 15 24 98 37 84 19 21 73 29 42 11 56 93 67 69 58 22 2 29 35 67 23 62 30 82 29 67 68 11 36 72 26 40 26 63 59 90 27 62 21 49 92 86 35 93 15 77 8624 64 34 0 93 50 41 28 64 88 79 55 21 51 46 45 18 ...

Send outputs with multiple variables from server to client in C

I want to send the printf's from my sorter function and main function as outputs to the client. I would really appreciate it if someone would tell me how to get the outputs on the client side instead of the server side since I tried using write() and vnsprintf() but i do not know how to send a string that contains variables in it using write() or vnsprintf(). For example
printf("Thread %d processes the %d array and the median is %.1fn",myStruct->threadNum, m + 1 + myStruct->arrayNum, median)
this code will print to the server terminal but I want it to print to the client terminal but since it has multiple variables, I do not understand how to pass it to the client.
SERVER CODE
/* A simple server in the internet domain using TCP
The port number is passed as an argument */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#define NUMTHREADS 4
///////////////////////STRUCTURE//////////////////////////////////
struct threadStruct {
int myArr[60][30];
int rowSizeLimiter[60];
int threadNum;
int arrayNum;
double medianSum;
};
/////////////////////QSORT COMPARE FUNCTION/////////////////////////
int cmpfunc (const void * a, const void * b) {
return ( *(int*)a - *(int*)b );
}
//////////////////////////////////FIND MEDIAN FUNCTION////////////////////////
double findMedian(int arr[], int arrLen) {
double median;
int mid, value;
value = (arrLen%2);
if(value == 1){
mid = (double)((arrLen/2) + 0.5);
median = arr[mid];
}
else{
mid = arrLen/2;
median = ((double)(arr[mid - 1] + arr[mid])/2);
}
return median;
}
//////////////////////SORTER FUNCTION/////////////////////////////
void* sorter(void *args){
struct threadStruct* myStruct = args;
int m, n;
double median;
int simpleArr[30];
myStruct->medianSum = 0;
for(m = 0; m < 60; m++){
for(n = 0; n < myStruct->rowSizeLimiter[m]; n++){
simpleArr[n] = myStruct->myArr[m][n];
}
qsort(simpleArr,myStruct->rowSizeLimiter[m], sizeof(int), cmpfunc);
for(n = 0; n < myStruct->rowSizeLimiter[m]; n++){
myStruct->myArr[m][n] = simpleArr[n];
}
median = findMedian(simpleArr, myStruct->rowSizeLimiter[m]);
myStruct->medianSum = median + myStruct->medianSum;
printf("Thread %d processes the %d array and the median is %.1f \n",myStruct->threadNum, m + 1 + myStruct->arrayNum, median);
}
printf("----------- Processes Finished for thread %d ----------- \n", myStruct->threadNum);
return NULL;
}
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno;
socklen_t clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr,
&clilen);
if (newsockfd < 0)
error("ERROR on accept");
bzero(buffer,256);
n = read(newsockfd,buffer,255);
if (n < 0) error("ERROR reading from socket");
int myArray[240][30];
int cellTracker[240]; //This array holds the amount of values stored in each row of the 2d array. Its purpouse is to give a fixed length for each row.
char line[240];
char *token;
double totalMedianSum;
int i ,z ,j, k, count;
k = 0;
struct threadStruct myStruct[NUMTHREADS];
pthread_t thread[NUMTHREADS]; //threadID initialization
FILE *fp;
//////////////////////////READ////////////////////////////////////////////////////
buffer[strcspn(buffer, "\n")] = 0;
fp=fopen(buffer, "r");
i = 0;
while(fgets(line, sizeof(line), fp)){
j = 0;
token = strtok(line, " ");
while(1)
{
z = atoi(token);
myArray[i][j] = z;
token = strtok(NULL, " ");
cellTracker[i] += 1;
if (token==NULL) break;
j++;
}
count = i + 1;
i++;
}
printf("The number of arrays is %d", count);
printf("\n");
///////////////////////////Passing cellTracker to rowsize limiter/////////////////
for(i = 0; i<240; i++){
if(k>59){
k = 0;
}
if(i>=0 && i<60){
myStruct[0].rowSizeLimiter[k] = cellTracker[i];
k++;
}
else if(i>=60 && i<120){
myStruct[1].rowSizeLimiter[k] = cellTracker[i];
k++;
}
else if(i>=120 && i<180){
myStruct[2].rowSizeLimiter[k] = cellTracker[i];
k++;
}
else if(i>=180 && i<240){
myStruct[3].rowSizeLimiter[k] = cellTracker[i];
k++;
}
}
///////////////////////////LOOP FOR SPLITTING ARRAY INTO 4 SMALLER ARRAYS/////////
for(i=0;i<60;i++){
for(k=0;k<cellTracker[i];k++){
myStruct[0].myArr[i][k] = myArray[i][k];
}
}
for(i=60;i<120;i++){
for(k=0;k<cellTracker[i];k++){
myStruct[1].myArr[i-60][k] = myArray[i][k];
}
}
for(i=120;i<180;i++){
for(k=0;k<cellTracker[i];k++){
myStruct[2].myArr[i-120][k] = myArray[i][k];
}
}
for(i=180;i<240;i++){
for(k=0;k<cellTracker[i];k++){
myStruct[3].myArr[i-180][k] = myArray[i][k];
}
}
myStruct[0].arrayNum = 0;
myStruct[1].arrayNum = 60;
myStruct[2].arrayNum = 120;
myStruct[3].arrayNum = 180;
///////////////////////Creating the threads////////////////////////////
for(i = 0; i< NUMTHREADS; i++){
myStruct[i].threadNum = i + 1;
pthread_create(&thread[i], NULL, sorter, &myStruct[i]);
pthread_join(thread[i], NULL);
totalMedianSum += myStruct[i].medianSum;
}
printf("\n");
printf("The sum of all median values is %.1f \n", totalMedianSum);
close(sockfd);
return 0;
}
CLIENT CODE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
void error(const char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
if (argc < 3) {
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
printf("Please enter the file name: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n",buffer);
close(sockfd);
return 0;
}
FILE CONTENT
1 98 48 55 46 12 48 43 7 73 47 33
97 26 67 25 67 47 46 74 67 43 78 98 90 58 6 5 53 19 37 42
81 84 36 70 59 65 99 56 94 23
79 73 27 47 98 78 8 2 3 43 84 19 59 98 51 72 2 13 55 3
20 61 25 84 28 70 56 23 52 69
86 70 60 90 72
51 71 92 91 55 6 72 67 73 64 78 43 65 30 49
99 82 40 72 5 44 20 21 22 72 99 74 27 59 25 1 26 26 22 66
63 27 54 46 46
91 65 77 45 94 94 95 11 77 46 64 14
69 70 75 48 68 22 55 78 1 4
31 76 55 12 93
15 28 25 91 44 5 67
81 50 84 9 26 29 98 5 86 92
60 54 52 66 49 22 73 47 84 82 84 63 12 12 19
27 34 89 16 41 85 56 76 1 49 74 80 59
68 75 95 21 25 29 49 27
16 99 68 11 10 49 90 57 84 98 56 88 19 66 44 14 32 35 17 93
30 95 24 23 60 94 28 27 10 98 11 52
73 24 76 72 86 79 74 50 59 58
81 25 48 57 62
16 50 78 84 32 14 37
76 49 20 28 92 55 34 92 14 43 13 27 64 95 91 2 14 49 93 32
1 18 79 79 92 15 42 71 72 52 35 26 96 87 97 58 33 5 2 45
37 69 46 6 67 15 89
30 14 36 22 26 96 72 34 82 65 69 50 98 3 67
16 36 64 41 71 64 96 90 50 74 36 65
37 70 4 36 68 22 65 41 65 90
100 90 85 90 30 91 9 99 70 90 35 96 98 29 13 83 65 44 58 70
93 62 29 95 6 66 1 53 28 72 21 5 36
65 31 40 58 96 89 31 1 66 31 31 81 10 15 86
26 62 35 70 7 71 73 94 29 100 36 47 34 97 73 22 60 93 82 55
98 72 21 47 48 88 87 31 28 44 62 47
82 63 39 23 7 79 51
35 70 58 84 48 65 58 30 43 43 31 52 12 23 3
49 56 50 92 41 50 36 49 31 79
77 2 57 94 85 97 87 36 59 45 10 46 55 51 71
70 47 10 35 75 44 54 93 46 27
22 51 84 98 6 73 70 73 28 57 42 85 53
36 24 88 16 64 6 22
43 95 87 92 98
77 14 32 19 44 7 32 48 65 47 42 69 76 62 100 51 66 78 1 24
32 66 96 1 45 86 98 52 31 17 13 97
95 68 19 79 89 23 53
6 2 46 61 86 87 79
56 99 67 31 41 45 42
65 98 14 76 24 23 36 38 43 33
31 60 72 41 89 95 67 42 51 77 12 46
76 91 11 60 92 63 92 21 27 43
95 14 63 40 78 25 42 100 42 12
24 35 27 20 49 45 73 58 2 11
16 34 83 12 42 4 84
83 48 61 84 58 31 56 81 8 93 55 69 99 98 82 11 75 52 1 91
35 3 98 85 57 50 9 62 24 65 99 49 65
100 61 49 67 49 70 13 56
44 34 5 79 80 43 99 77 24 71 79 85
15 60 24 61 4 58 69 97 60 93 90 49 62 30 39 69 72 50 30 36
98 6 98 36 79 72 30 74 45 43 14 73 90 5 91
51 4 1 96 68 68 68 35 5 3 91 87 65
1 33 8 4 16 85 22 46 39 81 94 5 73 57 92
28 91 79 93 46 53 49 73
31 51 23 23 8 98 6 77
84 67 55 25 52
11 57 7 80 58 60 74 85 68 19 64 10 48 48 9
62 30 55 69 91
86 97 20 47 11 85 27 79 83 69 45 23
75 65 60 95 100 25 59
97 62 14 60 63 46 34 96
13 32 49 75 12 18 87 4 10 56 68 95 90 38 65
56 43 74 27 3 88 67 75 91 33 100 58 68 72 67 58 83 1 79 28
43 5 57 14 54
4 50 47 96 98 95 37
76 32 97 41 90 48 97 39 97 7
75 16 7 95 5 85 64 10 98 70 4 60 79 82 42 40 85 67 8 43
56 5 52 73 56 78 44 76
12 2 14 46 65 97 62 33
62 52 61 63 36
32 53 79 15 76 96 45 17 16 72
79 86 41 21 4 38 64 99 79 38 54 58 100 31 39 88 41 49 11 37
90 12 87 99 97 72 87 68 84 15 100 98 45 62 9 62 99 33 3 17
34 45 72 65 3 89 79 83 50 81 46 82 54
12 68 69 36 8 4 70 63 18 12
51 74 4 60 58 28 69 43 26 96 80 34 45 88 14 23 4 92 69 28
87 8 87 74 68 84 82 27 58 86 21 27 18
67 58 17 38 35 99 19 61
98 42 97 52 39 73 33 75 36 66 63 5 31
91 52 99 73 84 24 32 57 77 91 61 24 42 42 70 2 5 55 22 45
41 9 43 53 59 60 48
49 2 85 53 25
6 28 17 6 88 71 62 43 19 68
52 96 31 92 68 26 88 34 49 54 3 50 50
59 92 17 77 66 59 35 6 49 6 74 99 65
1 7 97 78 18 2 2 99 78 82 1 44 53
39 63 86 88 38 48 58
59 72 37 80 25 32 56 37 24 82 74 85
48 14 64 9 64 97 60 63 25 85 42 96 70 55 56 94 1 29 16 54
51 41 21 71 60 75 55
33 37 32 43 49 69 56 93 4 1 82 60 93
71 59 98 79 67 2 86 76 87 1 49 66
100 59 56 94 4 44 66 6 22 23 19 46 56 7 23 23 71 97 10 10
62 37 86 57 64
45 62 80 49 66 86 97 64
41 26 82 38 45 32 24 97 45 82 23 72 90 11 67
16 56 1 6 85 89 92 12 43 50 99 73
60 27 21 40 47 68 58 35 89 37 51 34 42 51 83 93 100 91 47 4
38 66 67 76 51 47 78 55 18 85 91 28
65 99 3 48 97 21 82
29 13 85 68 37 84 37 8 45 94 26 51 40 27 68 31 45 78 52 50
66 74 67 81 74 40 46
23 80 42 33 57 8 46 21 78 99
31 11 96 37 21
36 97 75 57 17 20 19 77 62 21 54 24 27 41 98 75 97 45 78 3
98 94 51 87 51 57 79 2 57 79 3 17 68 32 27
35 50 10 90 65 63 61 80 72 5 32 16 19
87 50 91 100 71 60 36 3 89 77
4 17 81 73 54 78 70 48 63 74 78 73 82 57 19 92 15 47 33 98
48 92 89 20 56 21 64 86 96 48 99 58 89 37 14
67 48 24 47 63 40 58 67 15 67 15 97 58
64 91 43 36 20 46 86
74 11 43 16 23 97 4 67 81 84 66 15 46 21 4 54 63 47 45 80
13 19 56 97 80 48 92 56 89 40 77 58 50 92 8
49 23 49 22 4 42 55 15 36 63 88 82
100 40 41 5 59 64 37 39 82 91 25 80 66 91 31 42 6 70 14 94
82 75 11 57 9 70 23 36
33 16 6 84 4 5 69 21 65 24 43 64 19
82 86 16 45 93 41 84 4 45 2 63 18 27 68 45
43 52 44 35 45 86 71 85 42 31 83 27
26 87 97 100 54
99 45 52 70 80 13 54 37 52 62 100 56
63 8 7 32 45 13 13 16 8 11 64 11
37 20 16 61 25
42 78 37 78 32 79 99 70 5 61 61 60 73 5 30
26 28 67 32 55 35 92
9 79 79 8 82
22 92 36 11 82 16 37
84 88 43 78 22 73 95 93 18 73 37 16
43 100 54 6 23 48 82 6 42 20
69 25 39 74 13
24 78 33 18 34 51 79 13 20 89 18 64 29 77 2
3 84 14 34 92
56 94 21 12 27 34 54 35 63 67 83 50
55 46 60 39 24 37 98 15 74 53
69 73 75 29 88 63 44 26 19 15 72 52 54 33 100
98 75 70 78 22 26 82 35 68 22 57 39 55 52 38
16 52 50 33 79 11 62 18 42 82
32 58 100 18 58 5 37 80
10 27 39 79 46 26 34 20 54 58 61 59 61
59 71 42 62 78 52 9
17 86 8 43 98
86 6 12 17 29 15 70 1 60 86 70 84 50 15 71
65 3 43 17 74 53 38 74 29 13 20 54
23 24 56 71 59 38 55 89 49 18 84 87 76 46 5
27 74 90 26 68 60 27
36 10 62 64 60 66 67 45 81 37
68 21 36 81 42 15 17 29 6 17 86 47 82 71 40 38 54 94 27 3
76 66 84 13 98 27 13 70
95 21 34 78 13 28 27 13 39 47 62 84 99 4 18
96 37 44 86 10 69 79 62
6 82 50 59 6 33 58 7 34 71 63 34 100
30 99 52 81 67
48 75 31 16 64 28 93 7
61 79 9 25 96 17 41 44
41 76 22 37 92 19 62 47 22 55 56 54 80
48 72 18 73 58 47 9 74 20 71 64 93 39 93 75 97 99 9 100 57
38 51 35 91 19 96 75 29 53 76 70 74 37 68 97 88 47 85 12 45
41 25 53 67 63
55 47 70 17 63 52 44 30 41 80 2 41 16
65 12 23 40 60 62 54 16
46 97 29 84 35 93 97
3 11 33 86 36 62 44 31 71 26
12 45 46 79 94 11 89 62 60 76 28 96 6 92 72 69 15 34 97 84
49 70 77 45 31 98 61 61
3 43 31 18 44 99 3
29 16 57 33 90 81 34 29 82 59 58 72
35 4 78 8 17 67 39 78
56 8 80 21 86 41 14 54 12 67 51 5 94
76 51 6 14 19 47 86 74 31 49 94 38 58
50 5 18 49 31 76 4
34 90 57 10 41 10 53
90 7 17 85 75 2 70 16 22 76 60 71 93
68 21 54 18 60 90 59 74 8 31 34 63 9
58 45 92 19 10 78 37 22 60 73 96 93
13 27 3 72 33 58 50
1 94 13 6 75 76 74
23 84 5 1 38 41 42 37 90 54 43 14
84 66 6 59 6 37 47 62
36 52 25 52 75 53 26 76
51 52 18 33 36 91 47 47 44 34 25 70
79 77 19 57 78 95 38 62
10 99 53 45 7 43 18 83
85 33 30 11 66 54 5 76 82 48 70 57 79 89 61
21 49 99 41 51 4 2 35 19 24 11 76 9
99 34 1 13 85 11 60 91
5 62 58 100 81
99 31 36 41 16 35 46 17 39 98 44 22
92 5 48 49 47 10 91
64 80 81 16 73 35 99 16 14 32
55 66 70 74 98 80 6 37 46 99 64 63 18 11 86
88 92 60 88 32 94 60 36 14 25 48 71
68 20 32 89 72 39 2 4 15 12 79 72
99 60 86 50 88
32 45 100 16 10 100 45 52 47 41 87 46 54 70 49
61 84 34 18 8
60 14 33 11 88
89 94 5 32 89 60 24 86 23 45 25 17
1 43 3 78 83 8 32 55 75 61 53 21 25
96 7 57 5 36 6 84 10 71 38 93 38 14 57 65
13 65 47 27 53 29 60 40 55 28 86 48 60
25 44 29 21 6 65 47
81 58 95 15 78 55 21 63 62 6
30 99 63 5 47 23 89 4 97 41 73 54 54
41 46 27 52 27
37 91 72 54 32
96 2 57 13 38 81 3 69 5 43
69 22 40 32 25
48 53 73 29 72 44 3 96 64 62 36 11 24 70 80
76 44 60 65 64 49 53 22
89 48 74 69 98 90 82 36 58 10 93 72
79 56 70 77 46 53 86 4 60 51 61 46 89
43 40 71 84 23 74 64 73
96 29 14 31 22 78 21 77
57 100 4 39 92 42 30 24
51 54 25 93 86 55 39 38 46 65 83 6
69 7 71 95 62 21 14
55 1 89 57 86
93 79 6 56 21 10 2 87
73 36 54 12 64 86 28 56 57 26
13 90 52 8 59 88 32 16 46 39 69 99 14 76 25
74 19 69 24 27 74 65 82 2 42 58 73 47 83 13 80 19 56 83 98
96 47 71 4 65 39 15 58 90 10 94 95 64
39 78 21 67 4
6 46 36 28 87 49 89 22 91 81
60 32 45 100 55 30 51 31 29 55
89 54 7 41 31 68 48 77
88 13 82 78 86
27 58 88 13 57 33 57 93 14 25 62 19 100 86 26
72 64 20 51 32 19 39 95 69 71
7 86 21 98 51 12 47 71
13 42 57 51 82 66 65 21 6 2 83 35 25 10 28
82 30 33 29 77 91 98 55 65 40 4 9 21

Printing from 1 to 100 in columns

Code:
for(int i = 1; i <= 100; i++){
if(i % 10 == 1)
printf("\n%d", i);
else printf("%4d",i);
}
Results:
(blank line)
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
How can I remove the first blank line without doing something like:
for(int i = 1; i <= 100; i++){
if(i == 1 || i % 10 == 1)
printf("\n%d", i);
else printf("%4d",i);
}
And how can I 'indent' the columns? As the last column isn't properly arranged, and I have no idea how to fix it using printf's.
At last, sorry for wrinting '(blank line)' as I don't know how to make a blank line, and thanks for the help.
Rather than printing a newline at the start of a line, print it at the end instead:
for (int i = 1; i <= 100; i++){
printf("%4d",i);
if (i % 10 == 0)
printf("\n");
}
This prints each number in a 4 character field, then prints a newline if the last number is divisible by 10.
Output:
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
The style is strange, a newline doesn't belong at the start of a string literal. A printf call should print the line and then output a newline if necessary:
"\nBad style"
"Good style\n"
To print the values correctly, change the logic. Always print the values of i, and then check if i is divisible by 10, and print the newline if it is.
for(int i = 1; i <= 100; i++)
printf("%*d%c", (i % 10 == 1)? 0 : (i == 2) ? 4 : 3, i, (i % 10) ? ' ' : '\n');
I have always done something like this:
#include <stdio.h>
int main (void) {
for (int i = 0; i < 100; i++) {
if (i && i % 10 == 0) putchar ('\n');
printf ("%4d", i + 1);
}
putchar ('\n');
return 0;
}
You can also you separate format strings to accomplish the same thing:
char *fmt = "%4d",
*fmtn = "%4d\n";
for (int i = 0; i < 100; i++)
printf ((i+1) % 10 == 0 ? fmtn : fmt, i + 1);
putchar ('\n');
(note: in both cases the loop is indexed from 0 to accommodate indexing arrays)
Example Use/Output
$ ./bin/arraycols
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100

How to copy an array

I have defined an array like this:
uint8_t brightnessvariants[4][101]={{1,2,...},{3,4,...},{5,6,...},{7,8,...}};
Now for my application i have to fill another array with one of the 4 arrays:
uint8_t brightnesstable[] = brightnessvariants[index][];
But that doesn't work. Index is counted from 0 to 3.
brightnesstable and brightnessvariants are defined in a header file as extern
How can I do it right?
Simply do
uint8_t brightnesstable[101];
memcpy(brightnessstable, brightnessvariants[index], 101*sizeof(uint8_t));
brightnessvariants[index] is the address of the first item in the (index+1)nth row and the number of bytes you want to copy is
ROWSIZE*sizeof(ITEM_SIZE).
It really depends on what you need to do. If you need to create separate storage for a duplication of one of the rows of brightnessvariants, then you can simply declare a separate array and copy the values as discussed in the comments and the other answer.
If, however, you simply need to access one of the rows of brightnessvariants in the current scope, then there is no need for separate storage and copying. All that is required is to declare a pointer and assign the starting address of the desired row. Then you can access the desired row of brightnessvariants as if it were a separate array. e.g.
uint8_t brightnessvariants[4][101] = {{0}}, *btable = NULL;
Above, btable (short for your brightnesstable) is simply a uint8_t pointer. It can be assigned the the address of any of the rows in brightnessvariants. e.g. for the second row
btable = brightnessvariants[1];
btable can then be used to access any value within the second row, just as if it were a separate array. Here is a short example that may help:
#include <stdio.h>
#include <stdint.h>
int main (void) {
uint8_t brightnessvariants[4][101] = {{0}}, *btable = NULL;
int nrows = sizeof brightnessvariants / sizeof *brightnessvariants;
for (int i = 0; i < 101; i++) {
brightnessvariants[0][i] = i + 1;
brightnessvariants[1][i] = i + 3;
brightnessvariants[2][i] = i + 5;
brightnessvariants[3][i] = i + 7;
}
for (int i = 0; i < nrows; i++) {
printf ("\nbrightnesstable[%d] :\n\n", i);
btable = brightnessvariants[i];
for (int j = 0; j < 101; j++) {
if (j && j % 10 == 0) putchar ('\n');
printf (" %3hhu", btable[j]);
}
putchar ('\n');
}
return 0;
}
Example Use/Output
$ ./bin/ptrtobtable
brightnesstable[0] :
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
101
brightnesstable[1] :
3 4 5 6 7 8 9 10 11 12
13 14 15 16 17 18 19 20 21 22
23 24 25 26 27 28 29 30 31 32
33 34 35 36 37 38 39 40 41 42
43 44 45 46 47 48 49 50 51 52
53 54 55 56 57 58 59 60 61 62
63 64 65 66 67 68 69 70 71 72
73 74 75 76 77 78 79 80 81 82
83 84 85 86 87 88 89 90 91 92
93 94 95 96 97 98 99 100 101 102
103
brightnesstable[2] :
5 6 7 8 9 10 11 12 13 14
15 16 17 18 19 20 21 22 23 24
25 26 27 28 29 30 31 32 33 34
35 36 37 38 39 40 41 42 43 44
45 46 47 48 49 50 51 52 53 54
55 56 57 58 59 60 61 62 63 64
65 66 67 68 69 70 71 72 73 74
75 76 77 78 79 80 81 82 83 84
85 86 87 88 89 90 91 92 93 94
95 96 97 98 99 100 101 102 103 104
105
brightnesstable[3] :
7 8 9 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45 46
47 48 49 50 51 52 53 54 55 56
57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76
77 78 79 80 81 82 83 84 85 86
87 88 89 90 91 92 93 94 95 96
97 98 99 100 101 102 103 104 105 106
107
Look things over and let me know if you have any questions. If you need an example of copying instead of the use of a pointer, let me know and I'm happy to help.

Runtime error in codechef practice questions

I'm getting "sigsegv" , a runtime error from the following code when i try to run it on codechef while the code works fine on my computer with various test inputs.I've also kept in mind the constraints given in the problem but I'm still unable to debug it. The question is not from any contest but a practice problem . Please point out any mistake you can find .
Actual codechef question
#include<stdio.h>
int cash[101][101]={0};
int rec[101][2];
int ri=0;
int sumx(int mat[101][101],int i,int j,int lines)
{
int n=0,a=0,b=0;
if(cash[i][j]!=0)
{
return cash[i][j];
}
else if(i==lines-2)
{
n=(mat[i+1][j]>mat[i+1][j+1])?mat[i+1][j]:mat[i+1][j+1];
cash[i][j]=n+mat[i][j];
rec[ri][0]=i;
rec[ri++][1]=j;
return n+mat[i][j];
}
else
{
a=sumx(mat,i+1,j,lines);
b=sumx(mat,i+1,j+1,lines);
n=(a>b)?a:b;
cash[i][j]=n+mat[i][j];
rec[ri][0]=i;
rec[ri++][1]=j;
return n+mat[i][j];
}
}
int main()
{
int i=0,k=0;
int lines=0,n=0;
int r=0;
int tc=0;
int mat[101][101];
scanf("%d",&tc);
while(tc--)
{
scanf("%d",&lines);
i=0;
k=0;
while(i<lines)
{
while(k<=i)
{
scanf("%d",&mat[i][k]);
k++;
}
k=0;
i++;
}
if(lines==1)
{
r=mat[0][0];
}
else
{
r=sumx(mat,0,0,lines);
}
i=0;
while(i<ri)
{
cash[(rec[i][0])][(rec[i][1])]=0;
rec[i][0]=0;
rec[i][1]=0;
i++;
}
ri=0;
printf("%d\n",r);
}
return 0;
}
The error is with lines
while(i<ri)
{
cash[(rec[i][0])][(rec[i][1])]=0;
rec[i][0]=0;
rec[i][1]=0;
i++;
}
the values of rec[i][0] rec[i][1] can be undefined in some cases ie they may return garbage values
You can Use memset instead to change the values to 0
memset(rec,0,sizeof(rec));
memset(cash,0,sizeof(cash));
I ran your solution , there's a bug in your algorithm implementation try finding it yourself
I can provide you a test case(using a testcase generator) for which it fails
21
79
89 28
14 6 63
96 58 67 48
80 8 22 27 8
24 21 23 96 97 72
38 90 95 83 57 60 94
13 96 9 24 65 27 67 40
26 20 58 42 29 8 52 49 37
80 65 65 34 79 10 89 11 20 84
57 59 72 79 51 67 84 70 43 62 96
16 4 18 9 5 40 34 2 15 4 28 50
29 1 60 39 28 92 38 65 95 57 10 71 37
25 78 96 43 17 51 88 19 0 30 20 80 39 35
55 41 63 76 4 20 97 72 43 93 76 11 82 33 25
61 85 41 77 42 90 20 5 69 51 4 54 41 18 83 72
12 56 21 82 7 1 84 26 47 26 22 52 84 39 75 70 89
12 39 83 92 49 20 35 20 31 96 66 75 48 79 13 51 49 50
42 81 0 58 70 40 16 83 27 34 79 64 14 26 19 22 38 55 93
64 81 26 29 47 22 73 61 3 2 61 99 18 43 33 10 13 46 24 53
5 56 0 0 3 0 71 12 82 34 17 11 14 51 1 82 73 53 85 75 89
correct answer is 1431 while your code returns 1299
#Randomizer : The code runs fine with the output as 1431 , not 1299 . Anyways , as you suggested about the garbage being generated from rec[i][0] rec[i][1], It never appeared to me that rec[i][0] rec[i][1] could be generating garbage as I only accessed the used up cells . However , memset eliminated the need of rec , so cash was easily reset to zero . All i did was remove the rec matrix and reset cash using memset and the code ran fine on codechef. I couldn't figure out how it could be generating garbage , still , its accepted on codechef since I removed rec part . Thank you for the help.

Resources