Adak 419 Nearly a Posting Virtuoso

It's really odd how having no code for upper or lower case changing, means no result. ;)

Adak 419 Nearly a Posting Virtuoso

PLease help me !!
how can i uppercase the half only of the string and the half is in lower case??

include string.h, and ctype.h, and use the strlen(string) function to help you know the length of your string. Then divide by two. Add one if the length is odd (13/2=6, but the other half will be 7, not 6 char's in length).

Then use a for loop from 0 to half-1:

for(i=0;i<half;i++)
  charArray[i] = toupper(charArray[i]);

Use similar logic for the other half, and tolower(), in the same way, starting at half and ending at length of the string in charArray[].

The way it works on the forum, is that you need to post up your code FIRST, and state what the problem is, if you want help that includes specific code.

And always put your code between code tags (click on

icon in the editor window)[code]
icon in the editor window)

Adak 419 Nearly a Posting Virtuoso

A couple big time savers:

1) You only need to check a number up to (and including) the square root of that number. If the number is prime at that time, then it's prime.

2) increment your loop by 2 for the numbers to be checked for primeness. Even numbers can never be prime if they are higher than 2.

You can read up on the Sieve of Eratosthenes at Wikipedia.

Adak 419 Nearly a Posting Virtuoso

All operators (and thus all mathematics operators), are tokens. Your compiler help should have a list of them.

Adak 419 Nearly a Posting Virtuoso

It's easier if you create the array in the calling function, then pass it into the called function as a parameter. Upon return from the called function, the array is still valid, and has the data you want in it.

You can do it the other way, but it's more work.

Splam Bub commented: Nice and helpful. +0
Adak 419 Nearly a Posting Virtuoso

ptr is being given a new address, in the assignment statement ptr=&val, in fn(). It no longer has anything to do with i.

Adak 419 Nearly a Posting Virtuoso

I used a char array
char filename, and a char *extension = ".txt".

I used an unsigned int for the number. As the program looped around for another filename increment, the file_num++, of course.

iota() from ctype.h, was then used to put the number into the filename char array. Then I used strcat() from string.h, to add the .txt onto the end of the filename array. You need to include both ctype.h and string.h.


FILE *fp = fopen(filename, "wt");
if(fp==NULL) {
  printf("Error opening %s\n", filename);
  exit() or return;
}

If you don't have itoa(), check your help files for things like ltoa() or stroi, etc. You can "peel" off the digits from the number and put them into the string by hand, but it's much more work.

Post up your attempt and we'll get you sorted out.


Edit: For a small number of numbered files, only:

Char's are just numbers with a very small range, so you can also increment the filename char's, directly:
filename[3]++; //etc.


The problem with this, is you have to handle every bit of the arithmetic, yourself (changing a 9 to a 10, leaving the zero there, and incrementing the adjacent element of the array, by one). If you have many numbers, forget this and use the way above.

Adak 419 Nearly a Posting Virtuoso

There were three bugs:

1) Gerard found the big bug, which was the improper initialization of the array values.

2) The index out of range bug

and

3) The < operator needed to be changed to > if you wanted the largest number in the array.

The #2 error was not causing a run-time problem, but you see this all the time with C programs. The pointer or index for the array, goes out of bounds.

Asserts can help keep that down, but everyone should know that special care must be taken to test that the index referencing the array, must be carefully tested at the low and the high ends of the array range, for this type of error.

Adak 419 Nearly a Posting Virtuoso

Sorry, mkab. That won't work.

j+1 is the standard C idiom used in bubble sort, and all kinds of other algorithms that check adjacent elements in an array.

if SIZE = size of the array, then

j = 0; j < SIZE; j++ is simply wrong.
j = 0; j < SIZE - 1; j++, then array[j] can be correctly compared with array[j+1]

Any reference to array is an error, in C, and no logic exists that will make it correct.

Yes, his program may run right, since errors in C do not guarantee a program will crash. It may even run correctly, for a long time (in some cases, the bug is never detected).

With a larger array, I believe I could crash his program in a heartbeat, but that's irrelevant. We want the logic to be correct.

Adak 419 Nearly a Posting Virtuoso

You're letting j run outside the boundary of the array on the last loop.

Keep it < 5, always. Also, < should be > in your comparison, if you want the largest number. Otherwise, you'll get the smallest number in the array.

Adak 419 Nearly a Posting Virtuoso

To fill the array with unique int's from 1 to 52, just use:

for(i=0;i<52;i++)
  array[i] = i+1;

Then you can swap random indeces around, until it's all shuffled

for(i=0;i<someNumberAtLeast25;i++) {
  temp = array[randomInt];
  array[randomInt] = array[randomInt2];
  array[randomInt2] = temp;
}

Give that a try. ;)

Adak 419 Nearly a Posting Virtuoso

There are four arrow keys. Each gives two bytes, the first one is 0 or 0x00 in hex.

The second byte determines which arrow key: 0x48, 0x4b, 0x4d, 0x50
for up, left, right and down, respectively.

You should download an ASCII char and key scan code chart, if you don't have one.

Adak 419 Nearly a Posting Virtuoso

One = char is for assignment in C. Two == char's are used for comparisons.

if(variable == someValue);

This is the pseudo code for a nested loop version of bubble sort:

procedure bubbleSort( A : list of sortable items )
  n = length(A)
  for (i = 0; i < n; i++)
     /* back through the area bringing smallest remaining element to position i */
     for (j = n-1; j > i; j--)
        if A[j-1] > A[j] then
           swap(A[j-1], A[j])
        end if
     end for
  end for
end procedure

Straight from Wikipedia, which has a whole page on it, and links to many more references, versions, etc.

Adak 419 Nearly a Posting Virtuoso

You've put in the wrong tags. For links, no tags are needed, can you fix it quickly?

After 30 minutes, you can't edit your post on the forum.

Welcome to the wonderful world of pointers/addresses running amok! ;)

Adak 419 Nearly a Posting Virtuoso

You can use a modified bubble sort if you make two changes:

When you find a char that needs to be swapped, you move the entire row of char's (the whole word).

Instead of comparing one column's char with the column next to it, like the regular bubble sort, you need to compare char's in the same column, but in adjacent rows.

IOW's, you "tilt" the array, over by 90 degrees, onto it's face, and make your comparisons, that way.

The sort through an index logic that WaltP mentioned above, is a great trick for any programmer to have in their toolbox, btw. It allows you to do some amazing tricks, since it keeps the original order of the input, yet allows you to SHOW the input, as if it was sorted, perfectly well (fast, too!).

Indexing is a powerful tool, any time you have a bunch of data, and need to organize it.

Adak 419 Nearly a Posting Virtuoso

I would ask this in one of the MS usenet groups (in newsgroups). This issue must be affecting others. Your answer may be there waiting for you, already.

In any case, you can get good answers, rather quickly there.

Good luck.

Adak 419 Nearly a Posting Virtuoso

Seems to be the same error - datacode[] is an array of type char, so using %c in fscanf() for it, is an error or warning (at least). Also, the & in front of datacode, is wrong, since the name of the char array, serves as the address to the base of the array.

You should use %s for a string, IF the string is terminated by an end of string char: '\0'. If it's just a collection of letters, then it should be terminated either with a char to mark it (like a comma), or a space (white space).

Adak 419 Nearly a Posting Virtuoso

Right off, there are no "string" data types in C. We use char array[sizeofArray];, instead.

Also, you need to use strcmp() to compare string in C. This

if(pos == string::npos)

Is an error for the compare, for the data type, and for the ::. There are no classes in C, and no ::

Try that and see if it looks better.

Adak 419 Nearly a Posting Virtuoso

The phrase "C does not want to play ball", is not nearly as helpful in troubleshooting your program, as you seem to believe it is. ;)

Use your debugger or add some printf() statements in your code, and give us the real problem you're seeing with your program.

Adak 419 Nearly a Posting Virtuoso

Each student will typically be taking several courses each semester, so adding more classes into your student struct, would not be unreasonable.

Two other alternatives:

1) Add a classes struct, and nest it inside your student struct.

The one I like best is an array of char, where each class is listed by it's abbreviation (standardized), and it's course number: so BIO110 refers to Biology 110, PHY212 to Physics 212, etc.

Now every class has exactly 6 char's (maybe you want 7 or 8 for some schools), and by simple division, you easily have the number of classes, and finding any class within this string for a database, would be easy.

I believe you're working toward this, are you not?

Joey_Brown commented: Adak, youve gave me a great idea m8! Thank you +1
Adak 419 Nearly a Posting Virtuoso

You made an array with only 1 row, 1 column, and 1 depth.

Since arrays in C always begin with subscript 0, trying to refer to [1] in any dimension, is junk - you're out of bounds. ;)

Adak 419 Nearly a Posting Virtuoso

The device you're working with, only works from top line to bottom line (unless you do some repositioning of the cursor, which is not needed here).

print the top line of *'s

in a loop:
print the left side * print the spaces needed, then print the right side *
continue printing the above sides of the rectangle, until you've reached the
full height of the diagram.
end loop

Now print the bottom line of *'s.

That is your pseudo code logic. The empty space inside the rectangle is the width of the rectangle - 2, of course.

Give that a shot! ;)

Adak 419 Nearly a Posting Virtuoso

Get a good IDE, and understand, it will take time and a lot of work to be a decent programmer.

Learning C from the command line -- holy shit! Insane confidence killer, for a beginner. imo.

Adak 419 Nearly a Posting Virtuoso

There we go! Thanks, Narue. I'd mentioned difftime(), but veered away from it because I didn't think it would handle years and leap years.

Heads up, Nick! ;)

Adak 419 Nearly a Posting Virtuoso

If you want to use tm, the system will handle leap years extra day, for you, IIRC.
Look at your help for the tm struct, and you'll see what you need to fill out. For my (older) struct, the tm is like this:

struct tm {
    int  tm_sec,  tm_min,  tm_hour;
    int  tm_mday, tm_mon,  tm_year; 
    int  tm_wday, tm_yday;
    int  tm_isdst;
  };

mday is "day of the month", 1-31.
wday is "day of the week" Sunday is 0
yday is "day of the year" 1-366
isdst is "is Daylight Saving Time, in your local time zone? 0 No or 1,Yes

Your answer can't be an int - (depending on your system and age, of course), make it an unsigned long int, if needed.

Adak 419 Nearly a Posting Virtuoso

These are the leap years we need to work with:

1972
1976
1980
1984
1988
1992
1996
2000
2004
2008
Adak 419 Nearly a Posting Virtuoso

OK, we can go this way, also.

A couple defines (or just data), to keep in mind:
SECSinHOUR 3600
SECSinDAY 86400

Adak 419 Nearly a Posting Virtuoso

Nope, on second thought I don't think difftime() is what you want. In my (old) compiler, I'd use two structs called "tm" (included in time.h). One would get my birthday data, the second one would get filled in by C itself, for now.

Then it's a matter of a little arithmetic, subtracting one tm struct members, from the other, and a bit of arithmetic on the resulting answer, and it's done. The big advantage of using the tm struct, is that the data is nicely laid out for you, for the current date/time, with it.

Let me know if you want to work with the tm struct, and need the struct definition, etc.

Sorry to confuse you, but I haven't done this kind of a program in a very long time. Lots of rust. ;)

Adak 419 Nearly a Posting Virtuoso

OK, are you looking to use difftime() ?

Here's an example, but I'm not exactly sure how it could fit into what you're doing - but it's very possible. ;)

#include <time.h>
#include <stdio.h>
#include <dos.h>
int main(void)
{
   time_t first, second;

   first = time(NULL);  /* Gets system
                           time */
   delay(2000);         /* Waits 2 secs */
   second = time(NULL); /* Gets system time
                           again */

   printf("The difference is: %f seconds\n",difftime(second,first));
   getch();

   return 0;
}
Adak 419 Nearly a Posting Virtuoso

Work it out, step by step. First, can you get the number of years, correctly?

What about the number of months, after the last full year?

Then get the number of days, after the last full month.

You see where I'm going with this: step by step, large to small:
The number of hours past the last full day,
The number of minutes after the last full hour
and lastly, the number of seconds after the last full minute.

Where are you stuck? ;)

Adak 419 Nearly a Posting Virtuoso

I'm saying I don't see how it can be right to print out result (which is?? the final answer in seconds I think), using %ld in printf().

Result is a char, and char's have a very small maximum value.

I'm approaching this from a "get the number of days, hours, minutes and seconds, and convert that entire amount to seconds", POV.

Adak 419 Nearly a Posting Virtuoso

I don't have that strptime() function, so I suggest:

1) Use an unsigned long for the seconds' data type. Int may over-run.

2) Once you get the number of days, (or whatever units you want to work with, just do the math yourself:

seconds +=hoursPerDay * secondsPerHour, + etc.

Adak 419 Nearly a Posting Virtuoso

"archive" is a string, and so scanf("%s", archive); is correct. Remove the ampersand from in front of your "archive", in that line of code.

Same is true for "original file", in line #9. Remove the ampersand.

The address will be correct, but the pointer will be of the wrong type.

You need three pointers to work with three files simultaneously. You are using four now, which is prone to errors, and confusing. Get the logic straight in your mind.

In the while statement, "n = " is unnecessary.

Adak 419 Nearly a Posting Virtuoso

Change your %f, %s, %s to %f%*c %s%*c %s

The %c handles the ',' char, and the * modifier in front of the c, should tell fscanf(), not to store the char.

fscanf(filePointerName, "%f%*c %s%*c %s ", &myFloatVariable, myString1, mySring2);
Adak 419 Nearly a Posting Virtuoso

There's no way to use a function that only takes one key input, and use it for to input an integer. No, since an integer might contain several digits.

You're asking if there is a trash can, that can haul the garbage truck. Clearly not.

Adak 419 Nearly a Posting Virtuoso

Why not have the user input an integer, instead of a char, (as mentioned above), and use a do while loop, instead of a while loop? That will put the test for what the user entered, AFTER they have entered the loop, instead of right at the top of it.

(This can be done with a while loop, but the logic has to be changed. It's more intuitive to use a do while loop, for clarity.)

Adak 419 Nearly a Posting Virtuoso

Without knowing you, your ability in C and the sciences, or your class, I don't see how we can be of much help.

The start must come from you! Look at things on sourceforge.net, and google up other idea's for a project, as well. Talk to your classmates, etc. What kind of projects have people in your level and school, done in the past? What are you interested in?

Get on it. We can help you with the code, but the initial idea -- no. Only you know yourself, and your instructor, and what kinds of things would be acceptable.

Adak 419 Nearly a Posting Virtuoso

If the test portion of the while(test in here) doesn't resolve to 1 (true), then the inner part of the while() loop will not be entered by the program.

So, is t[jcs] > 100, right at the top of the first loop?

And somewhere in the while() loop, you want to increment a variable (i is the standard syntax, but any integer variable name will work:

i=0;
t[i] = 99;
while(t[i]<100) {
  printf("i = %d", i);
  ++i;
  t[i] = getchar();
}

Without the ++i, the program would always put one value, right on top of the other one, in t. Be careful not to confuse the value of t with the value of i.

Welcome to the forum, TomaCukor! ;)

Adak 419 Nearly a Posting Virtuoso

I suggest using chess fonts - google, download them.

For a 2D chess board, a console window will be OK, but the visual quality of the pieces really suffers if you don't have chess fonts installed.

Using something like Winboard (a chess program interface for chess playing programs without them), could be an easy option.

For all things chess computer related:

openchess.com and talkchess.com

Adak 419 Nearly a Posting Virtuoso
# include <stdio.h>
# define MAX 100
int main(void)
{
    int oddcount, evencount;
    int array[MAX],i,n;

    printf("\n Enter Array Size: ",MAX);
 
    scanf("%d", &n); // User enters Array Size
 
    for(i = 0; i < n; i++)
    {
       printf("\n Enter %d value: ",i+1); 
       scanf("%d", &array[i]); // Allows user to enter values into the Array
       }
    
    putchar('\n');
    printf("\nArray Values: ");
    for(i = 0; i < n; i++)
    {
       printf("%3d ",array[i]); //Prints out the array -- this is where program stops it should carry on down 
    }
    oddcount = 0;
    evencount = 0;

    for (i = 0; i < n; i++) // Code determines wheter odd or even.
    {
      if (array[i]%2 != 0)
      {
        printf("\nThe number %d Is Odd.\n", i);
        oddcount++;     
      }
      else {
        printf("\nThe number %d Is Even.\n",i);
        evencount++;
      }
    }
    return 0;
}

Your code isn't stopping. It just finishes - you never added the print statement for how many even and odd numbers you had in the array.

Add that, and it should be fine - if not, post back. Don't you know how to single step through your program yet?

That's a MUST to learn, and right away. You have some real work to do to get yourself up to speed on this. Get cracking, dude! ;)

Edit: With n, you aren't setting up the size of the array - it has MAX number of elements. You are setting up how many elements your data will have, in the array. You don't want to go working with …

Adak 419 Nearly a Posting Virtuoso

You need opening and closing braces around the body of the last for loop.

Opening - line 29
Closing - on line 43

Any multiple line statement block of code, requires braces around it.
For example:

for(i=0;i<3;i++) {
  printf("\nThis is loop number: %d", i+1);
  printf("And this is being printed only once without the braces");
}
Adak 419 Nearly a Posting Virtuoso

Mosaic has kindly made several suggestions about your code problems.

Let's look at your code.

# include <stdio.h>
# define MAX 100
int main(void)
{
    int oddcount, evencount;
    int array[MAX],i,n;

    printf("\n Enter Array Size: ",MAX);
 
    scanf("%d", &n); // User enters Array Size
 
    for(i = 0; i < n; i++)
    {
       printf("\n Enter %d value: ",i+1); 

     // things like /n don't go inside scanf()
       scanf("%d", &array[i]); // Allows user to enter values into the Array
   }
    
    putchar('\n');
    printf("\nArray Values: ");
    for(i = 0; i < n; i++)
    {
       printf("%3d ",array[i]); //Prints out the array
    }
     //**************************************//
  
    // whether the values in the array are odd or even.
    for (i = 0; i < n; i++) // This section of the code is meant to determine 
       

/* 8888888888888888888888888888888888888888888888888888
move these two lines to ABOVE the start of this for loop it's inside of now 
    oddcount = 0;
    evencount = 0;

They don't belong down here.
8888888888888888888888888888888888888888888888888888
*/
       if (array[i]%2 != 0)  //removed your semi-colon
       {
          printf("\nThe number %d Is Odd.\n", i);
          oddcount++;     
       }
       else {  //added a brace
          printf("\nThe number %d Is Even.\n",i);
          evencount++;
       }  //and it's matching brace

    } //end of your for loop
    getch();
    return 0;
  }
}

That has several corrections and a note for you to move some assignments, so make that move of the assignments, and see if that isn't better.

You'll want to add the print statement for the number of odd and even numbers that were found, near the end of the program,

Adak 419 Nearly a Posting Virtuoso

You and your semi-colons!

if (array[i]%2 != 0); //delete this semi-colon!
Adak 419 Nearly a Posting Virtuoso

The semi-colon inside the << >> is incorrect. Delete it.

the red closing brace is something that will cause a lot of headaches if you keep putting them on the end of a line of code.

Don't do that -- they get over-looked. Employer's dislike it, for that reason.

Adak 419 Nearly a Posting Virtuoso

A couple of things:

for (i = 0; i < n; i++) << ; >>   // This section of the code is meant to determine 
                               // whether the values in the array are odd or even.
 
//remove the above semi-colon from the for loop 

    oddcount = 0;
    evencount = 0;[B]}[/B]

Don't put closing braces on the ends of the line of code. Closing braces go ONLY directly underneath (in the same column), as the opening brace OR the first letter of the line of code that they are part of:

for(......) {
  //other code here
}
//or

for(......)
{
   //other code here
}
Adak 419 Nearly a Posting Virtuoso

Needs a bit of touch up:

Basically this is the code but its not displaying my array just some random numbers.

Let's use MAX of 10, so you don't have to enter lots of numbers yet ;)

# include <stdio.h>
# define MAX 10
int main(void)
{
    int array[MAX],i,n;
    /* your array size is already set at MAX integers (0 through MAX-1) */
    //printf("\n Enter Array Size: ",MAX);
      
    for(i = 0; i < MAX; i++)
    {
       printf("\n Enter %d value: ",i+1); 
       scanf("%d", &array[i]);
    }
    //now print out the array
    putchar('\n');
    for(i = 0; i < MAX; i++)
    {
       printf("%3d  ",array[i]); 
    }
    getch();
    return 0;
}

By convention return 0 means run was normal. Anything else may be an error.

Adak 419 Nearly a Posting Virtuoso

Greater than 0, not less than:

while ((n = fread(buffer,1,sizeof buffer, original_pointer)) < 0)
moroccanplaya commented: explains stuff verywell, and he is one clever guy +1
Adak 419 Nearly a Posting Virtuoso

Go through the array twice:

First time, count the number of times each letter appears. That's easily done using logic like this:

for each row
  for each columm
    ascii[this letter's ascii value]++, coded like:
    ascii[myLetter]++;
  end for
end for

(a 256 element array of type int, will do nicely for ascii[].)

Now when complete, go through the ascii[] array, and find the largest values(s), and assign them to your maxLetter1, maxLetter2, etc., variables.

Then go back through your array, and mark out the letter which equal maxLetter1, or maxLetter1 and maxLetter2, etc.


Sounds like it would take a long time, but it doesn't because the small array easily fits in cache memory -- Zoom! ;)

Adak 419 Nearly a Posting Virtuoso

Hop in your time machine. Set the date for about 1999. Look into WindowsME, and earlier versions of Windows. After that, the NT kernel came into use from the server line, and that pretty much ended these kinds of shenanigans.

Adak 419 Nearly a Posting Virtuoso

Thanks Adak,
The angle is given in radians and the tolerance is (+/-0.05 rad). I am using it for the FFT Function, I posted in my other post about the malloc function, and I am using it for processor on a development board.
How would I fill the array though? It would be really long to fill it manually, unless of course I use the sin() function once to fill it. My question is then, how would I access it? My angle value will be some decimal number, how do I link it to the closest angle given in the table? Would I need to round the angle to the nearest angle provided in the array?
Thanks

You might load a look up table from a file, or have it calculated when the program first starts up, but probably the most common way is to simply assign the table with values.

No, I've never seen a look up table filled manually -- you're cracking me up here ;)
It could happen, odd though it sounds. Wouldn't advise it though.

This is an example of several look up tables, from a Sudoku program (it's not mine):

/****************************************************************\
**                                                              **
**  BB_Sudoku  Bit Based Sudoku Solver                          **
**                                                              **
**             Copyright (c) 2008, Brian Turner                 **
**             All rights reserved                              **
**                                                              **
**  BB_Sudoku_Tables.h : This file contains the various tables  **
**    used by the solver:                                       **
**                                                              **
**  General purpose tables: …