#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    74
    Rep Power
    2

    I need help in ARRAY


    Code:
    #include <stdio.h>
    
    	char pname[100][100];
    	int counter;
    	int wtime;
    	int tatime;
    	int numpro;
    	float avgWT;
    	float avaTAT;
    	int btime[100];
    	int atime[100];
    
    void get_data()
    {
    	int var;
    	
    	printf("ENTER NUMBER OF PROCESS: ");
    	scanf("%d", &numpro);
    	system("cls");
    	
    	for(counter=1; counter<=numpro;counter++)
    	{
    		printf("ENTER NAME FOR PROCESS: ");
    		scanf("%s", &pname[var]);
    		printf("ENTER ARRIVAL TIME FOR PROCESS %s:", pname[var]);
    		scanf("%d", &atime[var]);
    		printf("ENTER BURST TINE FOR PROCESS %s: ", pname[var]);
    		scanf("%d", &btime[var]);
    		system("cls");
    	}
    	
    }
    
    void display_data()
    {
    	int var;
    	printf("PROCESS\t\tARRIVAL TIME\t    BURST TIME\n");
    	for(counter=1; counter<=numpro;counter++)
    	{
    		printf("%s\t\t%d\t\t    %d\n", pname[var], atime[var], btime[var]);
    	}
    }
    main()
    {
    	get_data();
    	display_data();
    	getch();
    }
    1. if i dont make the global declaration pname a 2D array, the program crashes. Why?

    2. i also have a problem displaying the inputted values, can you help me determine what is wrong in it? it only displays the last inputted values from the last process' values.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4
    your concepts of global and local variable are not clear u need to study that first then u will understand what are the problem in ur program.

    to use array as local variable u need to pass it to other function.
    and there are various ways to pass and use 2d array but u first clear your concepts of local and global variable and then if u found any problem then ask.

    var is local variable and u have not initialized or assigned any value to it.
    that is creating problem in displaying data

    Comments on this post

    • jwdonahue disagrees : Please use a spell checker on your posts and concentrate a bit more on your grammer. Your posts are barely comprehendable.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Crashes? Or doesn't compile? If, as eramit2010 surmises, you are not passing the array when declared as local, then the program shouldn't even compile.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    74
    Rep Power
    2
    Originally Posted by dwise1_aol
    Crashes? Or doesn't compile? If, as eramit2010 surmises, you are not passing the array when declared as local, then the program shouldn't even compile.


    yep! it crashes, i was able to compile and run it. thanks for the answers.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Originally Posted by Kuiva
    yep! it crashes, i was able to compile and run it. thanks for the answers.
    With or without warnings?

    It looks like you are saying that the only difference between the version that crashed and this one is whether pname was declared as local or global. That is all the information that you are giving us.

    Did you get warnings with the version that crashed?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    74
    Rep Power
    2
    Originally Posted by dwise1_aol
    With or without warnings?

    It looks like you are saying that the only difference between the version that crashed and this one is whether pname was declared as local or global. That is all the information that you are giving us.

    Did you get warnings with the version that crashed?
    no warning at all, it compiles fine and run. just after entering a process name for the first process. that is where it crashes. whether i make this pname a local or global, as long as it is not in 2D array form the program crashes. another thing is, when i make pname a 2D array as local, it does not crash but the entered value for process name will display as (null)


    using Orwells DevC++ as an IDE/COMPILER.
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Post the code.

    DevC++ uses MinGW gcc, as do I. Others have posted code that threw warnings all over the place while DevC++ reported none.

    Post the code.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    74
    Rep Power
    2
    1D array pname as global variable, compile-run-value entered-crashes
    Code:
    #include <stdio.h>
    
    	char pname[100];
    	int counter;
    	int wtime;
    	int tatime;
    	int numpro;
    	float avgWT;
    	float avaTAT;
    	int btime[100];
    	int atime[100];
    
    void get_data()
    {
    	int var;
    	
    	printf("ENTER NUMBER OF PROCESS: ");
    	scanf("%d", &numpro);
    	system("cls");
    	
    	for(counter=1; counter<=numpro;counter++)
    	{
    		printf("ENTER NAME FOR PROCESS: ");
    		scanf("%s", &pname[var]);
    		printf("ENTER ARRIVAL TIME FOR PROCESS %s:", pname[var]);
    		scanf("%d", &atime[var]);
    		printf("ENTER BURST TINE FOR PROCESS %s: ", pname[var]);
    		scanf("%d", &btime[var]);
    		system("cls");
    	}
    	
    }
    
    void display_data()
    {
    	int var;
    	printf("PROCESS\t\tARRIVAL TIME\t    BURST TIME\n");
    	for(counter=1; counter<=numpro;counter++)
    	{
    		printf("%s\t\t%d\t\t    %d\n", pname[var], atime[var], btime[var]);
    	}
    }
    main()
    {
    	get_data();
    	display_data();
    	getch();
    }
    1D array pname as local variable, same result with 1D array global
    Code:
    #include <stdio.h>
    
    	int counter;
    	int wtime;
    	int tatime;
    	int numpro;
    	float avgWT;
    	float avaTAT;
    	int btime[100];
    	int atime[100];
    
    void get_data()
    {
    	int var;
    	char pname[100];
    	
    	printf("ENTER NUMBER OF PROCESS: ");
    	scanf("%d", &numpro);
    	system("cls");
    	
    	for(counter=1; counter<=numpro;counter++)
    	{
    		printf("ENTER NAME FOR PROCESS: ");
    		scanf("%s", &pname[var]);
    		printf("ENTER ARRIVAL TIME FOR PROCESS %s:", pname[var]);
    		scanf("%d", &atime[var]);
    		printf("ENTER BURST TINE FOR PROCESS %s: ", pname[var]);
    		scanf("%d", &btime[var]);
    		system("cls");
    	}
    	
    }
    
    void display_data()
    {
    	char pname[100];
    	int var;
    	printf("PROCESS\t\tARRIVAL TIME\t    BURST TIME\n");
    	for(counter=1; counter<=numpro;counter++)
    	{
    		printf("%s\t\t%d\t\t    %d\n", pname[var], atime[var], btime[var]);
    	}
    }
    main()
    {
    	get_data();
    	display_data();
    	getch();
    }

    any suggestion on what compiler should i use to replace Orwell DevC++?
  16. #9
  17. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    The first listing:
    C:TEST>gcc -Wall kuvia1.c
    kuvia1.c: In function `get_data':
    kuvia1.c:19: warning: implicit declaration of function `system'
    kuvia1.c:25: warning: format argument is not a pointer (arg 2)
    kuvia1.c:27: warning: format argument is not a pointer (arg 2)
    kuvia1.c: In function `display_data':
    kuvia1.c:40: warning: format argument is not a pointer (arg 2)
    kuvia1.c: At top level:
    kuvia1.c:44: warning: return-type defaults to `int'
    kuvia1.c: In function `main':
    kuvia1.c:47: warning: implicit declaration of function `getch'
    kuvia1.c:48: warning: control reaches end of non-void function

    C:TEST>
    No warnings, huh? What do you call those?

    Breaking those down:
    kuvia1.c:19: warning: implicit declaration of function `system'
    kuvia1.c:47: warning: implicit declaration of function `getch'
    #include the header files. Why don't you #include the header files that you need?

    kuvia1.c:25: warning: format argument is not a pointer (arg 2)
    kuvia1.c:27: warning: format argument is not a pointer (arg 2)
    kuvia1.c:40: warning: format argument is not a pointer (arg 2)
    Line 25: printf("ENTER ARRIVAL TIME FOR PROCESS %s:", pname[var]);
    Line 27: printf("ENTER BURST TINE FOR PROCESS %s: ", pname[var]);
    Line 40: printf("%s\t\t%d\t\t %d\n", pname[var], atime[var], btime[var]);

    You are telling printf to print a string, so it expects you to give it a string, which would be a char pointer. Instead, you only give a single char, pname[var]. My interpretation of what happens then is that that char value is used as a pointer (since you're telling the compiler that that's what you're giving it), which, being less then 256, is almost guaranteed to point to a memory location belonging to the OS, who then terminates you with the extreme prejudice that you so richly deserve for such an audacious access violation.

    Originally Posted by Henry Spencer
    If you lie to the compiler, it will get its revenge.
    kuvia1.c: At top level:
    kuvia1.c:44: warning: return-type defaults to `int'
    kuvia1.c: In function `main':
    kuvia1.c:48: warning: control reaches end of non-void function
    You are implicitly declaring main to return int, which is also an implied promise to the compiler that you will indeed return an int. Then you break your promise.

    Originally Posted by Henry Spencer
    If you lie to the compiler, it will get its revenge.
    Stop lying to the compiler. Always explicitly declare everything; never ever depend on implicit declarations. These are the skeletons for main() that you should always use:
    Code:
    int main()
    {
    
        return 0;
    }
    and
    Code:
    int main(int argc,char *argv[])
    {
    
        return 0;
    }
    No exceptions!

    For the second listing:
    C:TEST>gcc -Wall kuvia2.c
    kuvia2.c: In function `get_data':
    kuvia2.c:19: warning: implicit declaration of function `system'
    kuvia2.c:25: warning: format argument is not a pointer (arg 2)
    kuvia2.c:27: warning: format argument is not a pointer (arg 2)
    kuvia2.c: In function `display_data':
    kuvia2.c:41: warning: format argument is not a pointer (arg 2)
    kuvia2.c: At top level:
    kuvia2.c:45: warning: return-type defaults to `int'
    kuvia2.c: In function `main':
    kuvia2.c:48: warning: implicit declaration of function `getch'
    kuvia2.c:49: warning: control reaches end of non-void function

    C:TEST>
    The warnings tell the same story as above. But what the warnings to not tell us here is that you are committing the logic error of declaring two different instances of pname as local variables of two different functions and wishing that the data written into the one will somehow magically appear in the other.

    A local variable only exists within the function that declares it, it only comes into existence when the function call is being set up, and it ceases to exist while the function returns. That means that the pname that you are trying to use in display_data() is completely different than the pname that was created and populated by get_data() and which ceased to exist long before display_data() was called.

    Solution: In order to use pname as a local variable, it needs to be declared locally in main() and then it needs to be passed as an argument to both get_data() and display_data().

    The information of whether you were passing that local array to the functions is what you were not giving us before, which is why I had to insist on you posting the actual code, so that we could actually discover what you did.
  18. #10
  19. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Originally Posted by Kuiva
    any suggestion on what compiler should i use to replace Orwell DevC++?
    Dev-C++ is not a compiler; it is an Integrated Development Environment (IDE). It uses a compiler, which is MinGW gcc, a Windows port of Linux' GNU C compiler, gcc.

    Years ago, I had installed Bloodshed's Dev-C++ and immediately abandoned the IDE because it was too buggy to be able to do anything (eg, in trying to create a Petzold-style Windows program, it attempted one build and forever thereafter the build control was disabled -- that was the final straw). However, I still have and frequently use the MinGW gcc compiler that had come with it. I use MinGW gcc from the command line and I use my favorite text editor to write and edit the source code. Works great!

    What I read of Orwell's work is that he had fixed a lot of bugs. Search through the main menu for an options dialog. In the options dialog there should be an option for turning warnings on or off. Turn warnings on. If there is also an option for the severity of warnings to be displayed, tell it to display all warnings (which is what -Wall means in gcc's command line) -- IOW, turn warnings up. Then test whether that option selection works. Users of Bloodshed's Dev-C++ have reported turning warnings on and still not seeing any displayed, but I don't know about Orwell.

    If you cannot get Dev-C++ to work, then simply open a console (AKA "DOS window", AKA "Cmd Prompt", AKA "the command line") and change the directory to where your source files are. Then compile your souce directly from the command line. Just remember that you will need to include the compiler's bin directory in the search path for it to work. Simple and direct.

    Others may suggest alternatives. I use Microsoft Visual Studio 2008 for my professional C++ work, while our C compilers are specialized cross-compilers for embedded work. Microsoft offers "crippled" versions of its compilers for free (ie, each IDE only supports one language and some of the high-end professional features are disabled), but a student can still do everything he needs to with them and much more. Plus they come with an excellent debugger, which is an essential tool. Look for Visual Studio Express.

    I would suggest that you start a new topic asking for suggestions.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    74
    Rep Power
    2
    wow! so much errors!i feel bad, took computer science cause i dreamed of becoming a good programmer and here i was! then im gonna study those you have written.

    imma try looking for that option that shows that errors. great help! thank you. DevC++ suites my taste so i pick it up

IMN logo majestic logo threadwatch logo seochat tools logo