Thread: 3-D arrays

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    90

    3-D arrays

    overall goal is to understand the use multi dimensional array

    For example, suppose


    5 stdudents is stuying in fisrt standard Whose marks are as follows 40, 65, 98, 47, 65.


    8 stdudents is stuying in second standard Whose marks are as follows 58, 23, 40, 80, 65, 98, 47, 65.


    10 stdudents is stuying in second standard Whose marks are as follows 47, 65, 58, 23, 40, 80, 65, 98, 47, 65.

    I want to store all student marks

    Can i use 3d array to store all student marks ?

    Is my program storing all student marks ?

    Code:
    #include<stdio.h>
    
    
    int main ()
    {
        int student[5][8][10] = {{40, 58, 47}, { 65, 23, 65}, {98, 40, 58}, {47,80, 23}, {65, 65, 40}, {0, 98, 80}, {0, 47, 65}, {0, 65, 98}, {0, 0, 47} {0, 0, 65} };
        
        return 0;
        
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I really don't think the array will really do what you want in the case given. And your sample array doesn't really match the data given.

    For example how do you know that the 5 students in the first use case are the same students in the second use case that has 8 students.

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    90
    Quote Originally Posted by jimblumberg View Post
    I really don't think the array will really do what you want in the case given. And your sample array doesn't really match the data given.

    For example how do you know that the 5 students in the first use case are the same students in the second use case that has 8 students.
    I tried to make an example As much as i can understand If I have an integer values like the marks of 5 students for first stranded, marks of 8 students for second stranded, marks of 10 students for student stranded,

    I can declare three arrays

    Code:
    #include<stdio.h> 
    int main ()
    {
    	int fisrt[5] = {40, 65, 98, 47, 65};
    	int second[8] =  { 58, 23, 40, 80, 65, 98, 47, 65};
        int third[10] = {47, 65, 58, 23, 40, 80, 65, 98, 47, 65};
    	
    	return 0;
    }
    My question was that apart from using the three arrays, can I not use the 3D array?

    Quote Originally Posted by jimblumberg View Post
    And your sample array doesn't really match the data given.
    .
    I did not understand where they doesn't matches

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 5 stdudents is stuying in fisrt standard Whose marks are as follows 40, 65, 98, 47, 65.
    > 8 stdudents is stuying in second standard Whose marks are as follows 58, 23, 40, 80, 65, 98, 47, 65.
    > 10 stdudents is stuying in second standard Whose marks are as follows 47, 65, 58, 23, 40, 80, 65, 98, 47, 65.
    You have 23 students in total.

    > int student[5][8][10]
    Now you have 480 student marks ( 5 * 8 * 10 )

    But then again, it's hard to see through all the typos to what you're getting at.
    Is the second "second" really meant to be third perhaps?

    Code:
    #define MAX_STANDARD 3
    #define MAX_STUDENTS 10
    int studentmarks[MAX_STANDARD][MAX_STUDENTS] = {
       { 40, 65, 98, 47, 65 },  // the remaining 5 slots will be 0
       { 58, 23, 40, 80, 65, 98, 47, 65 }, // the remaining 2 slots will be 0
       { 47, 65, 58, 23, 40, 80, 65, 98, 47, 65 },
    };
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2019
    Posts
    90
    Quote Originally Posted by Salem View Post
    >

    But then again, it's hard to see through all the typos to what you're getting at.
    look at new example, program to store temperature of three cities of a week

    given data

    City 1, Day 1 = 33
    City 1, Day 2 = 34
    City 1, Day 3 = 35
    City 1, Day 4 = 33
    City 1, Day 5 = 32
    City 1, Day 6 = 31
    City 1, Day 7 = 30


    City 2, Day 1 = 23
    City 2, Day 2 = 22
    City 2, Day 3 = 21
    City 2, Day 4 = 24
    City 2, Day 5 = 22
    City 2, Day 6 = 25
    City 2, Day 7 = 26


    City 3, Day 1 = 24
    City 3, Day 2 = 22
    City 3, Day 3 = 21
    City 3, Day 4 = 22
    City 3, Day 5 = 22
    City 3, Day 6 = 23
    City 3, Day 7 = 26

    Code:
    #define  max_cities 3
    #define  temperature 7
    
    
    int main 
    {
    int temp[3][10] = { {33,34, 35 33, 32, 31, 30}, {32, 22, 21, 24, 22, 25, 26}, { 24, 22, 21, 22, 22, 23, 26} }
    return 0;
    }
    The first range says the array has three elements The second range says that each of these three arrays is an array of seven elements.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Right, so you've got 2D arrays figured out.

    A 3D array would be something like
    Code:
    int marks[YEARS][COURSES][STUDENTS];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-11-2013, 10:57 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  5. separating line of arrays into array of arrays
    By robocop in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2001, 12:43 AM

Tags for this Thread